Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NullPointerException on main class  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to change a jtextfield from java main class, but what I got is Exception in thread "main" java.lang.NullPointerException message.
first time I write and test my java application is no problem, but after some time I add some function, I got that message.
I am so sorry I am new here, please let me know if I make a mistake like a name of class or something else.
this is my code :



on line txtqtytimb.setText(a); is the problem, I don't know is this make you understand or not, so if you need some more my code just let me know, I'll provide it for you..
thank you in advance..
cheers,
Dwi
 
Saloon Keeper
Posts: 4101
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is txtqtytimb an instance variable (aka field)?
Is it initialized?

You can put a print statement in to see what txtqtytimb is.
 
Sheriff
Posts: 23491
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Is txtqtytimb an instance variable (aka field)?



It can't really be an instance variable, because it's being used in a static method and the compiler doesn't complain. So it's more likely to be a static variable.
 
Paul Clapham
Sheriff
Posts: 23491
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, Dwi Ahmad!
 
Dwi Ahmad
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Is txtqtytimb an instance variable (aka field)?
Is it initialized?

You can put a print statement in to see what txtqtytimb is.



txtqtytimb is a jtextfield and yes it is nitialized,

on initcomponents()


in the end of code


want another clue? I really have no idea about this error, I've been trying to find some stuff on google but I got nothing
 
Dwi Ahmad
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:And welcome to the Ranch, Dwi Ahmad!



thanks to welcoming me, though..
I really feel like I am here now..
 
Dwi Ahmad
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Carey Brown wrote:Is txtqtytimb an instance variable (aka field)?



It can't really be an instance variable, because it's being used in a static method and the compiler doesn't complain. So it's more likely to be a static variable.



no it isn't.. its a jtextfield
 
Paul Clapham
Sheriff
Posts: 23491
46
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've showed us some code which initializes it, but you haven't showed us whether that code is ever run or not.

It's very hard to discuss code when you can't see it. So having to ask questions and getting answers which just raise more questions is not very efficient. It would help if you just posted your code.

Hopefully you haven't got a GUI with lots and lots of components, because that just make a lot of irrelevant code for people to go through. For this problem you should have a GUI with only that one text field, to simplify the process of fixing the problem.

And by the way setting a component in the main method isn't the normal way to do things. If it's part of initializing the GUI then it should be in the code which initializes the GUI. So probably you wouldn't have this problem if you did things in the ordinary way. But it's worth fixing the problem anyway, just for learning about how things work.
 
Dwi Ahmad
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dwi Ahmad wrote:

Carey Brown wrote:Is txtqtytimb an instance variable (aka field)?
Is it initialized?

You can put a print statement in to see what txtqtytimb is.



txtqtytimb is a jtextfield and yes it is nitialized,

on initcomponents()


in the end of code


want another clue? I really have no idea about this error, I've been trying to find some stuff on google but I got nothing



is this not what you need?
 
Paul Clapham
Sheriff
Posts: 23491
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dwi Ahmad wrote:is this not what you need?



No, read my earlier post (which probably I was editing while you were typing).
 
Dwi Ahmad
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:You've showed us some code which initializes it, but you haven't showed us whether that code is ever run or not.

It's very hard to discuss code when you can't see it. So having to ask questions and getting answers which just raise more questions is not very efficient. It would help if you just posted your code.

Hopefully you haven't got a GUI with lots and lots of components, because that just make a lot of irrelevant code for people to go through. For this problem you should have a GUI with only that one text field, to simplify the process of fixing the problem.

And by the way setting a component in the main method isn't the normal way to do things. If it's part of initializing the GUI then it should be in the code which initializes the GUI. So probably you wouldn't have this problem if you did things in the ordinary way. But it's worth fixing the problem anyway, just for learning about how things work.



oh I see,
my program run perfectly except that code showing me that error.
I will try to explain to you why I write code that way so I hope you can give me another way instead of my unusual way.
I use this code :



I try communicate my java program with serial port of my digital scale,
when I try to move my the code to initComponents(); it doesn't show error but the main window won't appear.
I hope you can tell me another way..
 
Paul Clapham
Sheriff
Posts: 23491
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can see why the main window doesn't appear when you put that code in the GUI-initialization routine: the loop starting at line 16 never terminates. And also, it's trying to update the GUI so it should be running on the Swing thread. Or at least, the parts which update the GUI should do that.

So, that's line 26 which needs to run on the Swing thread. I see you already know how to do that, because you already did it at line 2.

Hope that helps.
 
Dwi Ahmad
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, I can see why the main window doesn't appear when you put that code in the GUI-initialization routine: the loop starting at line 16 never terminates. And also, it's trying to update the GUI so it should be running on the Swing thread. Or at least, the parts which update the GUI should do that.

So, that's line 26 which needs to run on the Swing thread. I see you already know how to do that, because you already did it at line 2.

Hope that helps.



how to make the line 26 run on the swing thread?
I don't know, because the line 2 is automatically there when I create new jPanel Form, please give me one more clue or example if you don't mind
 
Paul Clapham
Sheriff
Posts: 23491
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 2, 3, and 4 collectively make line 3 run on the Swing thread. So do the same thing to line 26.

Have a look at the API documentation for java.awt.EventQueue if it isn't clear to you what's happening there.
 
Dwi Ahmad
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Lines 2, 3, and 4 collectively make line 3 run on the Swing thread. So do the same thing to line 26.

Have a look at the API documentation for java.awt.EventQueue if it isn't clear to you what's happening there.



I got it, thanks a lot..
that java.awt.EventQueue is makes code inside it run on swing...

the code become :
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!