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 3 of 3

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    hi guys, i started studying java recently and today i'm stucked on this error.
    can anyone help me?

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    import java.lang.Integer;
     
    public class Matrix {
        public static int[][] matNodi() throws NumberFormatException, IOException
        {
        	String file = "/home/marco/file.txt";
        	int matNodi[][] = null;
            FileInputStream inputStream = new FileInputStream(file);
            DataInputStream in = new DataInputStream(inputStream);
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
     
            int lineCount = 0;
            StringTokenizer str;
        	String line = "";
            while ((line = bf.readLine()) != null) 
            {
        		str = new StringTokenizer(bf.readLine());
            	for ( int j = 0 ; j < str.countTokens(); j++ ){
            		matNodi[lineCount][j]= Integer.parseInt(str.nextToken());	
            	}
     
                lineCount++;
            }
            bf.close();
            return matNodi;
    	}
     
            public static void main(String[] args) throws NumberFormatException, IOException{
    		int [][] m = matNodi();
    		System.out.print(m[1][1]);
    	}
    }

    Exception in thread "main" java.lang.NullPointerException
    	at Matrix.matNodi(Matrix.java:44)
    	at Matrix.main(Matrix.java:53)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    The error message says that there was a variable with a null on line 44 when that line was executed. Look at that line, find what variable was null and backtrack in the code to see why that variable does not have a valid value.

    The code appears to read from the file in two places. The record read into the line variable is not used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    tuocuggino (April 30th, 2014)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Exception in thread "main" java.lang.NullPointerException

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    import java.lang.Integer;

    public class ExceptionDemo
    {

    public static int[][] matNodi() throws NumberFormatException, IOException
    {
    String file = "D:\\Backup\\a.txt"; //a.txt file is local file in that some test.
    int matNodi[][] = null;

    FileInputStream inputStream = new FileInputStream(file);
    DataInputStream in = new DataInputStream(inputStream);
    BufferedReader bf = new BufferedReader(new InputStreamReader(in));

    int lineCount = 0;
    StringTokenizer str;
    String line = "";
    while ((line = bf.readLine()) != null)
    {
    str = new StringTokenizer(line);
    for ( int j = 0 ; j < str.countTokens(); j++ )
    {
    matNodi[lineCount][j]= Integer.parseInt(str.nextToken()); // This point null pointer exception occured.
    System.out.println(matNodi[lineCount][j]); // Here I try to print matNodi but before it print occured exception.
    }

    lineCount++;
    }
    bf.close();
    return matNodi;
    }
    public static void main(String[] args) throws NumberFormatException, IOException
    {
    int [][] m = matNodi();
    System.out.print(m[1][1]);
    }
    }

    In above program , in for loop at "matNodi[lineCount][j]= Integer.parseInt(str.nextToken()); line NullPointerException occuured. And at line "str = new StringTokenizer(bf.readLine());" It should be as str = new StringTokenizer(line); otherwise in str goes from second line.

  5. The Following User Says Thank You to mittal For This Useful Post:

    tuocuggino (April 30th, 2014)

Similar Threads

  1. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM