What i'm trying to do here is just place the values in my text file into variables

the txt file contains:
1111
50
2222
60
3333
70
4444
80
5555
90

i get the error :Exception in thread "main" java.lang.NullPointerException
at udptry2.m.readfiless(m.java:41)
at udptry2.m.main(m.java:68)

package udptry2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author JAY THE KIDD
 */
public class m 
{
    BufferedReader in;
    BufferedWriter out;
    int a;
    String[] id;
    String[] result;

    public void openfile()
    {
        try{
            FileReader inputFile = new FileReader("serverdata.txt");
            in = new BufferedReader(inputFile);
        }catch(Exception e)
            {
        System.out.println(e +"File not found");
            }//end of catch
    }//end of open file

     public void readfiless() throws Exception
    {
        try {
            for(int i = 0;i<10;i++)
            {
            id[i]=in.readLine();
            
            result[i]=in.readLine();
          
            }

        } catch (IOException ex) {
            System.out.println(ex);
        }

        for(int i = 0;i<10;i++)
            {
            System.out.println(id);
            System.out.println(result);
            }

    }

    public void closefile() throws IOException
    {
        in.close();
    }//end of close

public static void main(String args[]) throws Exception
      {
    m r = new m();
    r.openfile();
    r.readfiless();
    r.closefile();
}

}

String[] result; declares a String array variable, but it doesn't create String array, so it's just a null pointer at this stage. When you try to use it you get as null pointer exception.
You must also create an empty String array to store values into with a
result = new String{size);
where size is how big you want the array to be.

Go at the line where the error tells you. You are using something there which is null. Something you haven't initialized. Also you cannot read a file if you don't know that it has enough lines in it.

String line = in.readLine();
while (line!=null) {
  System.out.println("Line read: "+line);


  // the last command of the while is:
  line = in.readLine(); 
  // you read the next line and go to the beginning of the while in order to check if it is null (no more lines to read)
}

Take the line and put it into your array. You need to check, the length of the array if it is enough to put elements inside. If the file has more lines than the length of the array

Try to change in these lines:::::

for(int i = 0;i<9;i++)// from 0 is your first line and 9 is the last line
            {
            id[i]=in.readLine();

            result[i]=in.readLine();

            }


for(int i = 0;i<10;i++)// from 0 is your first line and 9 is the last line
            {
            System.out.println(id);
            System.out.println(result);
            }
commented: Completely wrong -3

That is completely wrong. Don't give advices if you don't know what you are talking about. All you did is repeat the poster's code. If you say that the 10 needs to be 9 (which is wrong), then why didn't you do the same at the second loop?

Sorry dude that was my wrong that i didn't change that code correctly...
Try following code...
The null pointer exception because of the size of the array you did't declared..

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
 
/**
 *
 * @author JAY THE KIDD
 */
public class m 
{
    BufferedReader in;
    BufferedWriter out;
    int a;
    String[] id = new String[10];
    String[] result = new String[10];
 
    public void openfile()
    {
        try{
            FileReader inputFile = new FileReader("serverdata.txt");
            in = new BufferedReader(inputFile);
        }catch(Exception e)
            {
        System.out.println(e +"File not found");
            }//end of catch
    }//end of open file
 
     public void readfiless() throws Exception
    {
        try {
            for(int i = 0;i<9;i++)
            {
            id[i]=in.readLine();
 
            result[i]=id[i];
 
            }
 
        } catch (IOException ex) {
            System.out.println(ex);
        }
 
        for(int i = 0;i<9;i++)
            {
            System.out.println(id[i]);//your mistake is that you are trying to print the id only, check your above code.
            System.out.println(result[i]);
            }
 
    }
 
    public void closefile() throws IOException
    {
        in.close();
    }//end of close
 
public static void main(String args[]) throws Exception
      {
    m r = new m();
    r.openfile();
    r.readfiless();
    r.closefile();
}
 
}

It is: for(int i = 0;i<10;i++) And the correct way would be: for(int i = 0;i<in.length;i++) Also the OP, better add some checks whether the file has at least 10 lines:

for(int i = 0;i<in.length;i++) {
  String line = in.readLine();
  if (line==null) { // no more lines to read;
    break;
  }
  id[i]=in.readLine();
  result[i]=id[i];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.