Java Problems: Null Pointer Exception
Quaunaut
The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
in Off-Topic
<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-->
<!--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
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.
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.
Find the line that is crashing and above it put an if statement to check that it is a valid object.
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.
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.
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.
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...
nullpo!
ps: did you chose to use java or is it for a class/project/experiment/harbl
<!--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-->
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.