• Post Reply Bookmark Topic Watch Topic
  • New Topic

SimpleGui1B exception errors  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm teaching myself java using the Head First Java 2nd Edition book. I typed in the SimpleGui1B example (see below) and got it to compile fine. However, when I run it I get a long list of exception errors. I include the first 8 or so below. I am using jdk1.6.0 on a Windows XP.

Thanks in advance!

Doug

<<<< code>>>




<<<< runtime error messages >>>>

C:\JavaFiles>java SimpleGui1B
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SimpleGui1B.actionPerformed(SimpleGui1B.java:26)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.ActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
...etc... for 19 more errors.

[edit]Add code tags. CR[/edit]
[ August 03, 2008: Message edited by: Campbell Ritchie ]
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Doug Esser:



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SimpleGui1B.actionPerformed(SimpleGui1B.java:26)


So from the bold you know the error comes from your actionPerformed method on line 26 of your source code.

There is only one line in the actionPerformed method so it isn't hard to find. If is the one that does button.setText(...).

A Null Pointer Exception occurs when you try to run a method on an object that hasn't been initialized. You are running a method on button, so why would button not be initialized...

Let's see, you have the button declared as an instance variable when you do this:
JButton button;
But there is no = sign, so it isn't initialized yet. Where do you initialize it.

Scanning...

In your method go you have:
JButton button = new JButton("Click Me");
And that is initialized because it has an = sign and a new operator. But that button only exists inside the method go(). A variable's scope only lasts as long as the closest enclosing {} brackets, which in this case are the brackets that make the go() method.

So the button you make new with text "Click Me" is a different button in a different scope then the one you define as an instance variable.

So it looks like your problem is that you want the button you make in the go() method to be the same button you use in the action performed method. So what you have to do is NOT declare a JButton named button in the go() method, but simply assign a new instance:

Because this is an assignment only, not a declaration, it will use the only other JButton button variable in scope, the object instance variable.


By the way:
I get a long list of exception errors. I include the first 8 or so below.

You actually included only 1 exception, which contained the message:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
And a Stack Trace, which is a list of all the method calls leading up to the error. This is important information because you can sift through the Stack Trace to find the specific call in your code that caused the error being described by the message.
[ August 03, 2008: Message edited by: Steve Luke ]
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

If you go through the beginner's forum you will find how very common questions about NullPointerExceptions (commonly called NPE) are.

Please use the CODE button below the message window when quoting code; I have added code tags to your post so you can see how much easier it makes it to read.

You haven't got 27 exceptions (be grateful for small mercies!), but one, and what you get out is a printout of how the Exception has propagated from where it occurred back to the JVM.

Please do a search for NullPointerException and you will find all sorts of helpful old threads, like here, here, and here. The fact that a reprobate called "Campbell Ritchie" posted in all those quoted threads is purely intentional!

In your case it gives you a hint where the problem occurs. The top line of the stack trace (the top line usually gives you the actual location of the problem) has "actionPerformed" in, so you look in the actionPerformed method, and there is only one line of code for an Exception to arise from, so that rather narrows the search!
A NPE means that something to the left of a . operator or to the left of a [ or to the right of a "throw" keyword or to the right of a = operator (when un-boxing) is null, and there is only one thing to the left of a . or etc.

You need to go through the code and check very carefully how many times the "button" appears. Also check how many times it is preceded with a class name (type).
Every time it is preceded by a type, it is a new declaration, so if you have "button" preceded by a type twice, you have two declarations.
If you have two declarations, the one which is a local variable takes precedence, which phenomenon is called shadowing.
If you really want two objects called "button" that is all right, but if you only want one, then you need to take the declaration off one of the two (probably the local one) by removing the type name before it.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

The problem is that you have 2 separate variables called "button." The first is declared as an instance variable of the class SimpleGui1B, and the second is declared as a local variable inside the method go().

The "button" variable inside the method go() is the one that's assigned to an instance of JButton, but the instance variable "button" still has its default value of null.

When you reference "button" from the actionPerformed method, the instance variable is the only one that's in scope, and that is a null reference.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! Steve beat me to it. But at least we all three agree about the location of the problem
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Damn! Steve beat me to it. But at least we all three agree about the location of the problem

Yeah, it sits there for an hour, and then suddenly gets 3 responses in as many minutes.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Yeah, it sits there for an hour, and then suddenly gets 3 responses in as many minutes.
That means Ranchers are subclasses of Bus.

 
Doug Esser
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy cow, lots to go over. Thanks all!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!