wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" Watch "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" New topic
Author

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
I can't figure out why I keep on getting the message Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.

I have a server client program where the server sends out a mathnumber to the client. The number is shown up on a gui textfield. I put in my answer and hit the sendbutton.

Here is part of the so called MathClientHandler class.


And here is the Client class which has an inner class:



And here is the gui class:


So, when I execute this I get the error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Lektion5.MathClientGUINy.actionPerformed(MathClientGUINy.java:147) <--- So, this is the problem
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)


Why do I get a NPE here?
There is something wrong with the line

mcn.sendAnswer(tal);

Why doesn't the code understand it, or mcn?

Could anyone please help me here?


Anders
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Anders Kviback wrote:I can't figure out why I keep on getting the message Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.

I have a server client program where the server sends out a mathnumber to the client. The number is shown up on a gui textfield. I put in my answer and hit the sendbutton.

Here is part of the so called MathClientHandler class.

So, when I execute this I get the error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Lektion5.MathClientGUINy.actionPerformed(MathClientGUINy.java:147) <--- So, this is the problem
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)


Why do I get a NPE here?
There is something wrong with the line

mcn.sendAnswer(tal);

Why doesn't the code understand it, or mcn?

Could anyone please help me here?


Anders



That is what debugging is for -- the JVM is complaining that it can't dereference something on that line because something is null. But what is null??? Put a couple of print outs at the line -- print out the value of mcn. print out the value of tal. are any of them set to null?

And also, don't assume the line number is exact -- it may be off a bit, so put some debugging lines to make sure you are where you think it is.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

mcn is null , where do you set it to not null ??

Just skimming the code but it just looks wrong .

... mcn is declared (null), you call mcn.sendAnswer(tal); at the offending line which is a null pointer exception.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Thank you for answering, but why is mcn declared null?
Could you please explain that?

Anders
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Yes, you are right. Forget my previous message.

Thank you-I have to look into the code again.

Thanks, Anders

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41085
    
  43
That's a question for you. It gets declared, but nothing is assigned to it, so it stays null. But you should know what should be assigned to it, because you put the declaration there, right?

I notice that in line 26 of MathClientNy you are instantiating an object that -at a quick glance- might fit there; is that the one that you want to use for "mcn"?


Ping & DNS - my free Android networking tools app
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
So, I've been working on this now. I believe that I managed to solve that problem.
I changed the way to solve the issue but now I've got stuck on another thing.

I have a server and a MathClientHandler class that is supposed to send out mathematical
numbers, for example 2/4.
On the other side I have a client MathClient that is supposed to put an answer to
these numbers.

The MathClientHandler can only send out numbers when the client has answered on them.
I e it has to wait until it is OK to send.
I have a class Numbergenerator that deliveres the numbers:

The MathNumber class looks partly like this:

And then part of the server class:



So, the problem now is that I get a NPE at line 55 in the MathClientHandler class
and I can't understand why.
And if I hide the statement, ie //operand = talSvar.getOperand();
I the get this result from the console:

Waiting for a new number to send

, i e operand = null, but how can it be null?

If I do a System.out.println("")
System.out.println("op1: " + nbr.getNumber1());
System.out.println("op2: " + nbr.getNumber2());
System.out.println("op: " + nbr.getOperand());

The result is for example
op1: 3
op2: 13
op: /

i e operand is not null.


What is wrong here?

As you can see I have three synchronized methods and two threads here. Is there something wrong with the synchronization?
Please, anyone?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41085
    
  43
Anders Kviback wrote:So, the problem now is that I get a NPE at line 55 in the MathClientHandler class
and I can't understand why.

Assuming that "Line 55" is exactly what is line 55 in what you posted -which I doubt because of your ".......more code " comment- then it would likely be the talSvar variable that contains null. The code you posted doesn't assign anything to it, but since that is not the entire code we can't really help with that.


And if I hide the statement, ie //operand = talSvar.getOperand();
...
, i e operand = null, but how can it be null?

If you comment out the statement that assigns something to operand, then it would be natural that operand is null, no?
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Thank you for answering,

In MathClientHandler line 11 it should be


(instead of tal, forget talSvar),
and then on line 55 it should be


And as you say it is not assigned anything to it(nbr).

As you can see MathNumber is created in class Numbergenerator. So, then it must be possible in some way to get it to the method
sendNumber() in MathClientHandler class. How do I do that?

On my other question I just tried to show what happens when I hide the statement.

//Anders


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41085
    
  43
That's hard to say, as I don't really understand how all these classes work together. There seems to be some thread synchonization, and also some socket communication. Are all classes running in the same JVM? Which class creates and/or calls which other class? Is it really necessary to create new MathNumber objects, or would calling its setNumber method with new parameters suffice?
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Sorry, I forgot some peace of code that you need to see: Here is more of the MathNumber class:

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
This is the way it works:
I start the Numbergenerator thread in MathServer class and the MathClientHandler thread. In Numbergenerator I then set the numbers to work with
through nbr.setNumber(nbr1, op, nbr1);
In sendNumber(), MathClientHandler I have to wait to send new numbers to the client before having answered the number quiz.
So, I have three synchronized methods(or, at least I believe they are synchronized). They are supposed to interact through the classes and threads
MathClientHandler(MCH) class and Numbergenerator class.

public synchronized void setNumber(int op1, String op, int op2) - MathNumber class

public synchronized MatteTal getNumber() - MathNumber class

public synchronized void sendNumber() - MCH


So;

sendNumber has to wait until it can send away its number. If I set the setNumber method, operand gets a value
and therefore the setNumber method waits. When I get the number from getNumber method the operand gets a null value.
In sendNumber I can't send the number away if operand is null(i e we don't have a number to send). So we wait in sendNumber.
The thing is that the number is set so operand shouldn't be null. But how do I write the code so that nbr gets a value
(because it has a value and I do not understand how to get it) and not null?


The server client communication works fine, it is all running in the same JVM. I have to have this constructor -
MathNumber() that creates the numbers.
I can send the whole code if you want to?
Maxim Karvonen
Ranch Hand

Joined: Jun 14, 2013
Posts: 101
    
  10
Hi, Anders. Your Numbergenerator does not make much sense. It creates a new MathNumber and sets it to a private variable. This new number cannot be accessed by other threads. Also you have too many synchronized methods for a simple "exchange" task. And these synchronized methods are in different objects. So, I can't say which of synchronized methods are related to a number-passing.

First thing to do is split data classes and logic classes. Let MathNumber be only a data container. Create another class to transfer data between objecst. You may use an already available BlockingQueue implementation with size 1 for that purpose. Or you may implement your own exchanger with two methods (put/get). This exchanger may be generic. This separation will help to see are transfer objects are properly shared between parts of program or not. Numbergenerator and consumer (mathClientHandler) should have reference to a same exchanger instance (probably passed as a constructor parameter to them).
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Hello, again!

Thanks for answering. I have now been working on this issue and I solved it by doing
Hereby the code stops and waits at the last line.

So now that works and now everything works except one thing;

the system freezes. When starting up a client gui and after entering the port numbers a new thread starts. A new thread does also mean that a new gui starts. So,

most of the time the system can generate a math number or two maybe three but then it freezes. There are a lot of different threads here, gui, thread and then the main.
Do you have any idea were to start looking for the problem. Or do you need more info?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 
Similar Threads
Can't see print message after while statement
NPE when trying to get size() in ArrayList
Can't get ImageIcon to show up from captcha
Swedish Language?
socket, gui, thread - problem