• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null point  RSS feed

 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a problem with my variable being null when i call it from other class.



So in this class i have a value for temperatura after i run program, but when NetBeans makes his automatic GUI class, he makes another 2 classes, one with gui and 1 with main method, or just overrides the 1st one. Anyway, i make a label there in gui, and try to put temperatura to be text of it, but if i put it like this



or just String temp = k.getTemperatura(); label.setText(temp); temp will be always null. Why is that, what is the problem, is it something about Threads?!

Thank you
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you get the text from the label? And why do you store it in a String and not an int or a double?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post listings that at least compile. There are things that just can't work (e.g. listing 1 line 4, listing 2 line 13).

When you start a new thread with Thread#start(), that method immediately returns. So when you get the computed value from a worker thread, you have to ensure that the worker thread finished it's job.
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet: I have a String temperatura, and whant it to be text od label. Why do i need to put it to be int?

Hauke: Now it can compile, in second listing, if i dont put thread and just make a method and call temperatura like this


so without thread, it still returns null, not a value witch i get in code from first listing!
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Kesic wrote:





Please could you explain why you start a new thread in the run method? Lines 15, 16, 18 & 19 seem quite useless to me.

In your second listing: You create a new Kosutnjak and immediately read the field, which has the default initialisation - null. No other operations there, so that's what to expect.

In the first listing you execute some more methods -that you don't show us- before reading the value. I guess the convert method sets the temperature.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Kesic wrote:Wouter Oet: I have a String temperatura, and whant it to be text od label. Why do i need to put it to be int?

Because I doubt that "asdf" is a valid value for a temperature. Which would be an invalid value for a double or an int.
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter: Ill pars it to int, its not a problem, as long as its not null

So the problem is here Kosutnjak k = new Kosutnjak(); I needed to call all methods in gui, and then offcourse i get a value. Thank you very much!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Kesic wrote:Wouter: Ill pars it to int, its not a problem, as long as its not null

So the problem is here Kosutnjak k = new Kosutnjak(); I needed to call all methods in gui, and then offcourse i get a value. Thank you very much!


As Maneesh Godbole stated in your similar post in the Swing forum, for us to help you best, you should create a small compilable and runnable program that we can run, test, alter and if possible, fix, an SSCCE. That advice is still good.

[edit] By the way, Hauke Ingmar Schmidt has identified what likely is the major problem and its likely solution. You would do well to re-read his last post.
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first listing you execute some more methods -that you don't show us- before reading the value. I guess the convert method sets the temperature.


Yes, and that works fine.

Please could you explain why you start a new thread in the run method? Lines 15, 16, 18 & 19 seem quite useless to me


I make that Thread so that i can refresh temperature every 20 seconds. I put thread to sleep for 20 seconds,and it does get a new value for temperatura, but i cant refresh label like this. Should i make that thread in gui class ( second listing), and if i do that, how can i make that thread(i tried in prikazi(), but it doesn have much point)?
Pete, ok next time ill make a runnable program, but now i cant becouse i solved the problem with null, and now its about refreshing label with threads, and i dont know how to make it work with smaller code.

Thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Kesic wrote:...and now its about refreshing label with threads, and i dont know how to make it work with smaller code.


That would be something worth learning. The Swing tutorials can show you how.
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
Ivan Kesic wrote:...and now its about refreshing label with threads, and i dont know how to make it work with smaller code.


That would be something worth learning. The Swing tutorials can show you how.


Ok, ill look into it and maybe try to build only one thread in gui class, and remove this one from Kosutnjak class.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a much stricter separation of concerns, i.e. which class is responsible for a task and separation of tasks. This makes building and understanding a system much easier; especially when a GUI is involved.

Wouter asked why the temperature is stored in a string. That is because a temperature typically is not a string but a numeric value. It is presented as a formatted string sometimes, but that should be done by the class that is responsible for the presentation.

Should a class that reads and converts values responsible for updating a label periodically? Seems unlikely.

A pattern you could look is "Model-View-Controller" (MVC). It is a standard pattern for coupling data holding classes and their respective views. It is widely used in Swing.
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your advices!
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway, ive solved my problem with one simple swing Timer class!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!