Results 1 to 15 of 15
- 06-17-2010, 10:54 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Value of one class not updated in another . Inheritance?
Hi,
I am developing a program in Netbeans that updates values of a JSpinner when a socket connection comes in.
In netbeans, when you create a java desktop application, it creates a Main java class (in my case PolAntApp.java) and the GUI where all Swing stuff is (in my case PolAntView.java). So I went to the main class and did
PolAntView view;
and then when I received the value from the socket in the JSpinner (Variable d)
view.d.setValue(d1);
Here is the sample of my code in the main class
Thank you in advanceJava Code:/* * PolAntApp.java */ package polant; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import org.jdesktop.application.Application; import org.jdesktop.application.SingleFrameApplication; public class PolAntApp extends SingleFrameApplication { @Override protected void startup() { show(new PolAntView(this)); } @Override protected void configureWindow(java.awt.Window root) { } public static PolAntApp getApplication() { return Application.getInstance(PolAntApp.class); } /** * Main method launching the application. */ public static void main(final String[] args) { launch(PolAntApp.class, args); ServerSocket ss = null; Socket s = null; try { ss = new ServerSocket(6666); } catch (IOException e) {} System.out.println("Listening on port 6666: " + ss); while(true){ try { s = ss.accept(); System.out.println("New connections accepted: " + s); new GestorPeticion(s).start(); s = null; } catch (IOException e) {} } } } class GestorPeticion extends Thread { PolAntView view; int d1; BufferedReader entrada = null; Socket s; public GestorPeticion(Socket s){ this.s = s; } @Override public void run(){ try{ entrada = new BufferedReader(new InputStreamReader(s.getInputStream())); String Dp = entrada.readLine(); System.out.println("D parameter: " + Dp); d1 = Integer.parseInt(Dp); view.d.setValue(d1); }catch(Exception e){} } }
- 06-17-2010, 11:06 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What is your question?
- 06-17-2010, 12:20 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
The line
Does not work, the value of the integer just does not update in the jSpinner.Java Code:view.d.setValue(d1);
My question is why it does not work?
- 06-17-2010, 01:15 PM #4
Is it because view is a local variable?
You have a lot of classes not part of standard java. What do they do?
For example what does the launch() method do?
- 06-17-2010, 01:27 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Then how do I fix the problem of a local variable?
The launch method is autogenerted by netbeans when creating a desktop application and it is for showing the gui.
I discovered that after the line:If I doJava Code:view.d.setValue(dp1);
It does not show me the value in the console.Java Code:System.out.println(dp1);
I just want to modify the value of the JSpinner that is on my other class (PolAntView.java) in other file, in the same package
- 06-17-2010, 01:33 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
If you want to update GUI components from a background process/thread, you should use a SwingWorker.
- 06-17-2010, 01:34 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Never do catch(Exception e){}
You should at least have
That way you can see if there were any exceptions .e.g NullPointer exceptions because some variables (e.g view) were never initialized.Java Code:catch(Exception e){ e.printStackTrace(); }
- 06-17-2010, 02:38 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Thanks,
After adding e.printStackTrace(); to the catch, when I run the program and send the socket, the program gives me an error:
Still I do not know how to solve. Swingworker... is this the solution?Java Code:java.lang.NullPointerException at polant.GestorPeticion.run(PolAntApp.java:105)
- 06-17-2010, 02:52 PM #9
What code is at line 105? An object there is null.at polant.GestorPeticion.run(PolAntApp.java:105)
Where is that object given a value?
Can the value be null if there is a problem that the code doesn't test for?
Where is the view variable in the run() method given a value?
- 06-17-2010, 02:57 PM #10
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Sorry, I forgot to say. Line 105 points to :
The point is that I want to give a value to the variable d just after I receive it from the socket connection, concretely in this lineJava Code:view.d.setValue(dp1);
Java Code:String Dp = entrada.readLine();
Even more I discovered that just doingwithout using the SetValue gives the same nullpointerexception... I am guessing that when I do PolAntView view; I am not doing inheritance of all public variables of the PolAntView.java?Java Code:System.out.println(view.d.toString());
Thank you againLast edited by cotarelo; 06-17-2010 at 03:03 PM.
- 06-17-2010, 03:05 PM #11
Where is the view variable in the run() method given a value?
- 06-17-2010, 03:17 PM #12
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Sorry, but I am newbie and I am a bit lost. I can initalize the view to null, but I still get nullpointer exception.
What I want to do is after I receive the value from the socket in the variable Dp, I want to update the content of a jSpinner named "d" (which is on the class PolAntView.java) with the value on Dp.
So, what I am doing is declaring at the begginning of the class: PolAntView view. That is the only way I know to access to jSpinner "d" and then I can modify its value with view.d.setValue(Dp).
Bbviously I am doing something wrong... I dont know how to do it in a different way...
Please illustrate me?Last edited by cotarelo; 06-17-2010 at 03:20 PM.
- 06-17-2010, 03:23 PM #13
That will do it for sure!initalize the view to null, but I still get nullpointer exception
To NOT get a NPE, give the view variable the address of an object.
Where is the an instance of the PolAntApp class? Some where you need code like:
<your variable here> = new PolAntApp(); // create a PolAntApp object
- 06-17-2010, 03:38 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You need to read up on some things before writing your program.
It usually works that way, i.e read some relevant material first then write the code.
articles that should help here are
Doing Swing right - Java insights
NullPointerException a.k.a NullReferenceException - Java insights
- 06-17-2010, 03:47 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
how to get the child class in inheritance?
By java_fun2007 in forum New To JavaReplies: 7Last Post: 09-29-2010, 09:35 AM -
batch updated
By swati.jyoti in forum JDBCReplies: 2Last Post: 06-09-2009, 11:33 PM -
DefaultListModel being updated in another class (by reference?)
By rickyoswald in forum Advanced JavaReplies: 3Last Post: 04-24-2009, 06:28 PM -
why my image is not updated ?
By aneuryzma in forum New To JavaReplies: 4Last Post: 08-15-2008, 12:08 AM -
Updated Forum Rules
By levent in forum Suggestions & FeedbackReplies: 1Last Post: 08-12-2007, 01:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks