Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: How to solve NullPointerException in runtime?

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default How to solve NullPointerException in runtime?

    Hi,
    I have small problem
    port1 = prijatyPaket2.getPort();
                 System.out.println(port1); //Itīs ok port number is in port1
                 cisloPort1.setData(port1);// Error java.lang.NullPointerException //But i donīt know why?
     
     
    class Port1 {
        private AtomicInteger portt1 = new AtomicInteger();
        public synchronized void setData(int portt1){
          this.portt1.incrementAndGet();  
     
        }
        public synchronized int getData(){
            return this.portt1.get();
        }
    Where is error thank you.
    Last edited by Deep_4; November 7th, 2012 at 01:09 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Error

    Hello Koren3,

    Is the error that you get java.lang.NullPointerException or are you talking about something else?

    I can't solve this problem without seeing all the code but NullPointerException only occurs when your application attempts to use null in a case where an object is required.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Error

    only this problem:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package serversynchro;
     
    import java.util.concurrent.atomic.AtomicInteger;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Lolek
     */
     
    public class Main {
     
        /**
         * @param args the command line arguments
         */
     
        public static void main(String[] args) {
            try {
                byte[] ip ={Network.ip(127),Network.ip(0),Network.ip(0),Network.ip(1)};
                InetAddress klient1 = InetAddress.getByAddress(ip);
                byte[] ip2 ={Network.ip(127),Network.ip(0),Network.ip(0),Network.ip(1)};
                InetAddress klient2 = InetAddress.getByAddress(ip2);
     
                Port1 nPort1 = new Port1();
                Port2 nPort2 = new Port2();
     
     
                Preposilani1 nPrepos1 = new Preposilani1();
                Preposilani2 nPrepos2 = new Preposilani2();
     
                Vlakno1 serverSocket1 = new Vlakno1();
                Vlakno2 serverSocket2 = new Vlakno2();
     
                System.out.println(nPort1.getData());
     
                serverSocket2.vlak2.start();
                serverSocket1.vlak1.start();
     
                Thread.sleep(60000);
     
                while(true)
                {
                    DatagramPacket odesilanyPaket2 = new DatagramPacket(nPrepos2.getData(), nPrepos2.getData().length, klient1, nPort1.getData());
                    serverSocket2.server2.send(odesilanyPaket2);
     
                    DatagramPacket odesilanyPaket1 = new DatagramPacket(nPrepos1.getData(), nPrepos1.getData().length, klient2, nPort2.getData());
                    serverSocket1.server1.send(odesilanyPaket1);
                }
            }             
            catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }        catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
     
             }    
     
        }
     
     
     
    class Vlakno2 implements Runnable
    { 
        private Port2 cisloPort2;
        private Preposilani2 prepos2;
        volatile DatagramSocket server2;
        Thread vlak2;
        Vlakno2()
        {
        vlak2 = new Thread(this,"Vlakno 2");
        }
        public void run ()
        {        
            try 
            {
                server2 = new DatagramSocket(4000);
                byte[] prijata = new byte[256];
                byte[] odeslana2 = new byte[256];
                byte[] ip ={Network.ip(192),Network.ip(168),Network.ip(1),Network.ip(101)};
                int port2=0;
                while(true)
                {
                 DatagramPacket prijatyPaket2 = new DatagramPacket(prijata,prijata.length);
                 server2.receive(prijatyPaket2);
                 port2 = prijatyPaket2.getPort();
                 System.out.println(port2);
                 cisloPort2.setData(port2);
                 String prijaty = new String(prijatyPaket2.getData());
                 System.out.println("Od 2. klienta:"+prijaty);
                 odeslana2 = prijaty.getBytes();
                 prepos2.setData(odeslana2);
     
                }            
            } 
            catch (IOException ex) 
            {
                Logger.getLogger(Vlakno2.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }
    }
     
    class Vlakno1 implements Runnable
    { 
        private Port1 cisloPort1;
        private Preposilani1 prepos1;
        volatile DatagramSocket server1;
     
        Thread vlak1;
     
        Vlakno1()
        {
             vlak1 = new Thread(this,"Vlakno 1");
        }
        public void run ()
        {  
     
     
            try 
            {
                server1 = new DatagramSocket(5000);
                byte[] prijata = new byte[256];
                byte[] odeslana1 = new byte[256];
                byte[] ip ={Network.ip(192),Network.ip(168),Network.ip(1),Network.ip(101)};
                int port1;
     
     
                while(true)
                {
                 DatagramPacket prijatyPaket2 = new DatagramPacket(prijata,prijata.length);
                 server1.receive(prijatyPaket2);
                 port1 = prijatyPaket2.getPort();
                 System.out.println(port1);
                 cisloPort1.setData(port1);     
                // ipAdresa1 = prijatyPaket2.getAddress();
                 String prijaty = new String(prijatyPaket2.getData());
                 System.out.println("Od 1. klienta:"+prijaty);
                 odeslana1 = prijaty.getBytes();
                 prepos1.setData(odeslana1);                        
                }            
            } 
            catch (IOException ex) 
            {
                Logger.getLogger(Vlakno1.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }
    }
     
     
    class Port1 {
        private AtomicInteger portt1 = new AtomicInteger();
        public synchronized void setData(int portt1){
            this.portt1.incrementAndGet();  
     
        }
        public synchronized int getData(){
            return this.portt1.get();
        }
    }
    class Port2 {
        private AtomicInteger portt2 = new AtomicInteger();
        public synchronized void setData(int portt2){
             this.portt2.incrementAndGet();  
     
        }
        public synchronized int getData(){
            return this.portt2.get();
        }
    }
    class Preposilani1 {
        private byte[] odeslat1 = new byte[256];
        public synchronized void setData(byte odeslat1[]){
             this.odeslat1=odeslat1;
     
        }
        public synchronized byte[] getData(){
            return this.odeslat1;
        }
    }
    class Preposilani2 {
        private byte[] odeslat2 = new byte[256];
        public synchronized void setData(byte odeslat2[]){
            this.odeslat2=odeslat2;
     
        }
        public synchronized byte[] getData(){
            return this.odeslat2;
        }
    }
    When program start everything is OK. But when Clien send datagram. Write this stuf.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Error

    try changing
    int port1;
    to
    int port1 = 0;
    shouldn't make a difference for the error you are getting but always need to be sure.

    PS Please post the server and client, that way we can complie and run both, so it will actually recreate the error for us.

    Cheers,
    Chris

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Error

    I tried it. It same. Code client:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package messenger;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Lolek
     */
    public class Main
    {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            try {
                     DatagramSocket Test = new DatagramSocket();
                     byte[] test = new byte[256];
                     String testik =""; 
                     test = testik.getBytes();
                     byte ip[] = { Network.ip(127), Network.ip(0), Network.ip(0), Network.ip(1)};
                     InetAddress adresa = InetAddress.getByAddress(ip);
                     DatagramPacket testDatagram = new DatagramPacket(test,test.length,adresa,5000);
                     Test.send(testDatagram);
                     Test.close();
     
                while (true)
                {
                    try {
                            BufferedReader cteni = new BufferedReader(new InputStreamReader(System.in)); //cteni z klavesnice(vytvoreni proudu dat)
                            String ukonceni = "/q";
                            DatagramSocket klient = new DatagramSocket(); //vytvoreni klientskeho soketu
                            try {
                                      InetAddress lokalni = InetAddress.getByName("localhost"); //ziskani lokalni IP adresy
                                      byte[] poslana = new byte[256]; //vytvoreni 256 bytoveho pole pro posilana data
                                      byte[] prijata = new byte[256]; //vytvoreni 256 bytoveho pole pro prijimana data
                                      String klavesnice = cteni.readLine(); //cteni po radku a prirazeni do k
                                      poslana = klavesnice.getBytes(); //prirazani dat do poslana
                                      DatagramPacket odesilanyPaket = new DatagramPacket(poslana, poslana.length, lokalni, 5000); //zabaleni pole bajtu k odeslani +definice cilove IP adresy a portu
                                      klient.send(odesilanyPaket); //odeslani paketu
                                      DatagramPacket prijatyPaket = new DatagramPacket(prijata, prijata.length); //zaobalnei pole bajtu k prijmu
                                      klient.receive(prijatyPaket);                        //prijimani dat                        
                                      String prijataData = new String(prijatyPaket.getData()); //ulozeni dat do retezce
     
                                      if (!prijataData.isEmpty())
                                      {
                                         klient.send(testDatagram);
                                      }
                                      System.out.println("Od serveru:" + prijataData); //Vypis prijatych dat od serveru
     
                                      if (prijataData.equals(ukonceni))
                                      {
                                             System.exit(0);
                                      }
                                      klient.close();
                                 } 
                            catch (IOException ex) 
                                 {
                                      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                 }
                         }
                    catch (SocketException ex) 
                         {
                             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                         }
                }
     
            } 
       catch (IOException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } 
     
     
     
     
        }
     
    }
    class Network {
     
    public static byte ip(int i)
    {
        return (byte) ((i > 127) ? i - 256 : i);
    }
    }
    Thanks a lot Chris and Don Carleone.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Error

    I can compile this fine but cannot make it produce the error. What do I need to do to get the NullPointerException?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Error

    First compile server and after compile client.

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Error

    Even playing with IP address etc I cannot get the erro, or any outputs. Could you print screen the output?

  9. #9

  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Error

    Well you don't even use the port1 integer you are trying to pass anyway, so one solution would be to remove it from the function call and the function prototype. Then re-run and see if you are still getting a problem

    Chris

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Error

    Quote Originally Posted by Freaky Chris View Post
    Well you don't even use the port1 integer you are trying to pass anyway, so one solution would be to remove it from the function call and the function prototype. Then re-run and see if you are still getting a problem

    Chris
    Yes the problem is with cisloPort1.setData(port1);

    port1 is null causing the NullPointerException.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  12. #12
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Error

    Quote Originally Posted by JavaPF View Post
    Yes the problem is with cisloPort1.setData(port1);

    port1 is null causing the NullPointerException.
    Thats what I thought, so via removing the need to send port1 as an argument, since it is never used it would remove the problem. However I still cannot work out why it would do this

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Error

    When I comment out cisloPort1.setData(port1);

    I get:

    Od 1. klienta:
    In the console and no errors.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  14. #14

    Default Re: Error

    Hi Koren3,

    The NULL pointer exception is because you never initialized these two vars: cisloPort1 and prepos1

    Go to class Vlakno1 and replace the first two lines with this:

    private Port1 cisloPort1 = new Port1();
    private Preposilani1 prepos1 = new Preposilani1();

  15. #15
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Error

    Thanks it solved.

  16. #16
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Error

    Nice one leandro!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  17. #17
    Junior Member
    Join Date
    Jul 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to solve NullPointerException in runtime

    I have started to solve the corners. It wasnt hard till I ended up with one corner twisted. Is that possible?
    /Andreas

Similar Threads

  1. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  2. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM
  3. [SOLVED] Interesting error cipher while sending message
    By Koren3 in forum Java Networking
    Replies: 0
    Last Post: April 29th, 2009, 09:54 AM
  4. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM
  5. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 07:58 AM