Java Problems: Null Pointer Exception

QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
<div class="IPBDescription">:(</div> Okay. Heres the code:

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class rpg extends JFrame {
private GridBagLayout createLayout;
private GridBagConstraints createConstraints;
private JLabel createTitle;
private JLabel longSpace;
private JLabel shortSpace;
private JTextField charname;
private JLabel strengthLabel;
private JLabel agilityLabel;
private JLabel intelligenceLabel;
private JLabel luckLabel;
private JButton strengthUp;
private JButton agilityUp;
private JButton intelligenceUp;
private JButton luckUp;
private JButton doneChar;

public rpg(){
 super("My RPG");
 
 createLayout = new GridBagLayout();
 setLayout(createLayout);
 createConstraints = new GridBagConstraints();
 
 createTitle = new JLabel("Create a Character");
 
 longSpace = new JLabel("                  ");
 
 charname = new JTextField(10);
 
 
 strengthLabel = new JLabel("1");

 
 
 strengthUp = new JButton("Strength");

 
 
 agilityLabel = new JLabel("1");

 
 
 agilityUp = new JButton("Agility");

 
 
 intelligenceLabel = new JLabel("1");

 
 intelligenceUp = new JButton("Intelligence");

 
 luckLabel = new JLabel("1");

 
 luckUp = new JButton("Luck");

 
 doneChar = new JButton("Next");

 createConstraints.fill=GridBagConstraints.BOTH;
 addComponent(createTitle,       3, 0, 1, 1);
 addComponent(shortSpace,        1, 0, 1, 1);
 
 addComponent(charname,          0, 1, 1, 1);
 addComponent(shortSpace,        1, 1, 1, 1);
 
 addComponent(strengthLabel,     3, 5, 2, 1);
 addComponent(shortSpace,        2, 3, 1, 1);
 
 addComponent(strengthUp,        5, 5, 1, 1);
 addComponent(shortSpace,        4, 3, 1, 1);
 
 addComponent(agilityLabel,      3, 6, 1, 1);
 addComponent(shortSpace,        1, 6, 1, 1);
 
 addComponent(agilityUp,         5, 6, 1, 1);
 addComponent(shortSpace,        1, 6, 1, 1);
 
 addComponent(intelligenceLabel, 3, 7, 1, 1);
 addComponent(shortSpace,        1, 7, 1, 1);
 
 addComponent(intelligenceUp,    5, 7, 1, 1);
 addComponent(shortSpace,        1, 7, 1, 1);
 
 addComponent(luckLabel,         3, 8, 1, 1);
 addComponent(shortSpace,        1, 8, 1, 1);
 
 addComponent(luckUp,            5, 8, 1, 1);
 addComponent(shortSpace,        1, 8, 1, 1);
 
 addComponent(doneChar,          8, 14, 1, 1);
 addComponent(shortSpace,        1, 8, 1, 1);
}

private void addComponent(Component component, int column, int row, int width, int height){
 createConstraints.gridx = column;
 createConstraints.gridy = row;
 createConstraints.gridwidth = width;
 createConstraints.gridheight = height;
 createLayout.setConstraints(component, createConstraints);
 add(component);
}

public static void main(String args[]) {
 rpg Rpg = new rpg();
 Rpg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 Rpg.setSize(475, 250);
 Rpg.setVisible(true);
}

}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

Heres the message when I run it(no errors in console when compiling):

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->Exception in thread "main" java.lang.NullPointerException
       at java.util.Hashtable.put(Hashtable.java:401)
       at java.awt.GridBagLayout.setConstraints(GridBagLayout.java:405)
       at rpg.addComponent(rpg.java:106)
       at rpg.<init>(rpg.java:68)
       at rpg.main(rpg.java:111)
Press any key to continue...<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

<!--emo&???--><img src='http://www.unknownworlds.com/forums/html/emoticons/confused-fix.gif' border='0' style='vertical-align:middle' alt='confused-fix.gif' /><!--endemo-->

Comments

  • ByekaByeka Name changed from Freak83 Toronto Join Date: 2003-03-13 Member: 14484Members, Constellation
    Making a game?

    Dunno, I'd help if I could but I'm only getting a 62 in my Java class. It's a common runtime error that's all I can remember.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    <!--QuoteBegin-FreakEightyThree+Apr 29 2005, 12:58 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (FreakEightyThree @ Apr 29 2005, 12:58 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Making a game?

    Dunno, I'd help if I could but I'm only getting a 62 in my Java class. It's a common runtime error that's all I can remember. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Indeed. I'm looking to see if anyone knows why I'm specifically causing it here.
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    edited April 2005
    You're trying to use the properties or methods of an object that isn't a valid object.

    Find the line that is crashing and above it put an if statement to check that it is a valid object.
  • CageyCagey Ex-Unknown Worlds Programmer Join Date: 2002-11-15 Member: 8829Members, Retired Developer, NS1 Playtester, Constellation
    edited April 2005
    You didn't initialize shortSpace before you passed it into addComponent(). That may be the issue, or it may be another uninitialized component. I can't tell which one is the actual error because I don't have line numbers to reference and don't feel like hand counting <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->.

    From looking at the stacktrace, I'm guessing that the line <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->addComponent(shortSpace,        1, 0, 1, 1);<!--c2--></td></tr></table><div class='postcolor'><!--ec2--> is line 68.

    In the future, the best way to track down this sort of error is to find the first line that refers to your code (in this case the call to addComponent) and work your way down the list until you find something that makes sense to you--then work back up until you can figure out the source of the issue.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    <!--QuoteBegin-Cagey+Apr 29 2005, 06:30 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Cagey @ Apr 29 2005, 06:30 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> You didn't initialize shortSpace before you passed it into addComponent(). That may be the issue, or it may be another uninitialized component. I can't tell which one is the actual error because I don't have line numbers to reference and don't feel like hand counting <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->.

    From looking at the stacktrace, I'm guessing that the line <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->addComponent(shortSpace,        1, 0, 1, 1);<!--c2--></td></tr></table><div class='postcolor'><!--ec2--> is line 68.

    In the future, the best way to track down this sort of error is to find the first line that refers to your code (in this case the call to addComponent) and work your way down the list until you find something that makes sense to you--then work back up until you can figure out the source of the issue. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Well, I thought they were all fine- but I saw the one on line 111(in main, the creation of Rpg), and thought that was it.

    I'll try that. Thanks all- I'll post for results.
  • ZelZel Join Date: 2003-01-27 Member: 12861Members
    if you wrote a little more organized it would be apparent that shortspace is not defined. is tehre any need for 50% of those variables? they seem to define labels that never need be accessed again.

    and make sure you use a text editor with line numbering avaliable, then youll quickly see that 111 is what called rpg constructor, which is on 68, which has a problem with addComponent on 106.
  • locallyunscenelocallyunscene Feeder of Trolls Join Date: 2002-12-25 Member: 11528Members, Constellation
    just doing a quick count you have 15 declared variables and 14 are initialized, cagey's analysis looks to be correct.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    <!--QuoteBegin-Zel+Apr 29 2005, 08:20 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Zel @ Apr 29 2005, 08:20 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> if you wrote a little more organized it would be apparent that shortspace is not defined. is tehre any need for 50% of those variables? they seem to define labels that never need be accessed again.

    and make sure you use a text editor with line numbering avaliable, then youll quickly see that 111 is what called rpg constructor, which is on 68, which has a problem with addComponent on 106. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Read again and you'll see I use every single one.

    Cagey's analysis is correct. Darn obscure exception names...
  • BlackMageBlackMage [citation needed] Join Date: 2003-06-18 Member: 17474Members, Constellation
    [insert "loljavasux" rant here]
    nullpo!

    ps: did you chose to use java or is it for a class/project/experiment/harbl
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    <!--QuoteBegin-Black Mage+May 2 2005, 08:30 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Black Mage @ May 2 2005, 08:30 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> class <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    <!--emo&::nerdy::--><img src='http://www.unknownworlds.com/forums/html/emoticons/nerd-fix.gif' border='0' style='vertical-align:middle' alt='nerd-fix.gif' /><!--endemo-->
  • TranquilChaosTranquilChaos Join Date: 2003-07-25 Member: 18425Members
    Hardly obsure. NullPointerException is caused when the program encounters a null value where there should be an object.
    Class classPointer = new Class();
    classPointer is just that, a pointer to an object. classPointer.method() executes the method() method of the Class object to which classPointer points. When classPointer points to nothing and the program tries to call an object method via classPointer, a NullPointerException is thrown. To prevent this use checks for null, (if statements or try/catches to prevent this exception where it might possibly occur.
Sign In or Register to comment.