This is the server code for the udp server/client program . There are no errors except when you run the program the ff error occurs:
Exception in thread "main" java.lang.NullPointerException
at udptry2.UDPServerbackup2.main(UDPServerbackup2.java:44)
Java Result: 1

the client side works how it should just this part of the program

package udptry2;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import java.lang.String;

class  UDPServerbackup2 
{
    
   public static void main(String args[]) throws Exception
      {
            Scanner x = null ;
         DatagramSocket serverSocket = new DatagramSocket(6666);
            byte[] receiveData = new byte[1024];
            byte[] receiveData2 = new byte[1024];
            byte[] sendData = new byte[1024];
            byte[] sendData2 = new byte[1024];
        //String Serverdata="1234";
        int sendGrade;
        int serverdata[] =  {1111,2222,3333,4444,5555};
        int serverdata2[] = null ;
        int grades[] = {50,60,70,80,90};
        int grades2[] = {50,60,70,80,90};
        boolean flag1 = true;
        int i;

        //readfile r = new readfile();
        //r.openfile();
        
        try{
              x = new Scanner(new File("serverdata.txt"));
             

        }catch(Exception e)
            {
        System.out.println("File not found"+e);
            }//end of catch

        while(x.hasNext())
        {

            for(int j=0;j<serverdata2.length;j++)
            {
             serverdata2[j] = x.nextInt();

             grades2[j]=x.nextInt();

             System.out.println("id: "+ serverdata2[j] +"result: "+ grades2[j]);
            }
        }//end of while
            x.close();
        
       // r.closefile();
        
        
        
        
        
        System.out.println("=============");
	System.out.println("UDP Server Starting");
	System.out.println("=============");
            while(flag1 != false)
               {

                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String(receivePacket.getData());
                  sentence=sentence.trim();
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  String ack = "Client....This is Server...connection established successfully ";
                  sendData = ack.getBytes();
                  DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);


                  DatagramPacket receivePacket2 = new DatagramPacket(receiveData2, receiveData2.length); //new buffer
                  serverSocket.receive(receivePacket2);
                  String idnumber = new String(receivePacket2.getData());
                  idnumber = idnumber.trim();


           int a = Integer.parseInt(idnumber);
           System.out.println("id number you typed was: " + a);


        for(i=0;i<serverdata.length;i++)
        {
        if (a == serverdata[i])
        {
                  System.out.println("Grade is: "+grades[i]+"%");
                  sendGrade = grades[i];

                  String sendGradestring = Integer.toString(sendGrade);
                  sendData2=sendGradestring.getBytes();

                  DatagramPacket sendPacket2 = new DatagramPacket(sendData2, sendData2.length, IPAddress, port);
                  serverSocket.send(sendPacket2);
           break;
        }

                   } //end of for

               }//end of while
      }//end of main


}//end of class

At line 43 you are using the for statement as

for(int j=0;j<serverdata2.length;j++)

but the variable 'serverdata2' is set to null at line 22.
This is causing the NullPointerException

This article has been dead for over six months. Start a new discussion instead.