Results 1 to 15 of 15
  1. #1
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Exclamation 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

    Java 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){}
                    }
    
    }
    Thank you in advance

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What is your question?

  3. #3
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    The line
    Java Code:
    view.d.setValue(d1);
    Does not work, the value of the integer just does not update in the jSpinner.

    My question is why it does not work?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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?

  5. #5
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    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:
    Java Code:
     view.d.setValue(dp1);
    If I do
    Java Code:
     System.out.println(dp1);
    It does not show me the value in the console.

    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

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    If you want to update GUI components from a background process/thread, you should use a SwingWorker.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Never do catch(Exception e){}
    You should at least have
    Java Code:
    catch(Exception e){
       e.printStackTrace();
    }
    That way you can see if there were any exceptions .e.g NullPointer exceptions because some variables (e.g view) were never initialized.

  8. #8
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Thanks,

    After adding e.printStackTrace(); to the catch, when I run the program and send the socket, the program gives me an error:

    Java Code:
    java.lang.NullPointerException
            at polant.GestorPeticion.run(PolAntApp.java:105)
    Still I do not know how to solve. Swingworker... is this the solution?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    at polant.GestorPeticion.run(PolAntApp.java:105)
    What code is at line 105? An object there is null.
    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?

  10. #10
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Sorry, I forgot to say. Line 105 points to :

    Java Code:
    view.d.setValue(dp1);
    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 line
    Java Code:
    String Dp = entrada.readLine();



    Even more I discovered that just doing
    Java Code:
    System.out.println(view.d.toString());
    without 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?

    Thank you again
    Last edited by cotarelo; 06-17-2010 at 03:03 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Where is the view variable in the run() method given a value?

  12. #12
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    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.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    initalize the view to null, but I still get nullpointer exception
    That will do it for sure!

    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

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You need to pass the PolAppAnt object created in the launch() method to the GestorPeticion constructor.

    The problem is that Netbeans has hidden away the creation of that object in the launch() method. Unless you can get at that object then this is not going to work.

Similar Threads

  1. how to get the child class in inheritance?
    By java_fun2007 in forum New To Java
    Replies: 7
    Last Post: 09-29-2010, 09:35 AM
  2. batch updated
    By swati.jyoti in forum JDBC
    Replies: 2
    Last Post: 06-09-2009, 11:33 PM
  3. Replies: 3
    Last Post: 04-24-2009, 06:28 PM
  4. why my image is not updated ?
    By aneuryzma in forum New To Java
    Replies: 4
    Last Post: 08-15-2008, 12:08 AM
  5. Updated Forum Rules
    By levent in forum Suggestions & Feedback
    Replies: 1
    Last Post: 08-12-2007, 01:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •