0

I'm fairly new to programming and I wanted to learn how to read data from a file. I watched this video and tried to copy the idea for my own purpose. https://www.youtube.com/watch?v=3RNYUKxAgmw

Whenever I try to run my main it says Error Line 22 - NullPointerException. I have a text file with numbers and I have checked if the names match. I simply do not understand why it says my file is empty.

package uge4;
import java.util.*;
import java.io.*;

public class ReadFile {

    private Scanner x;
    public void openFile () {
        try {
            x = new Scanner (new File("gradeconverter.txt"));
        } catch(FileNotFoundException e){
            System.out.println("File not found.");
        }
    }

    public void readFile() {
        Scanner input = new Scanner(System.in);
        System.out.print("Insert old grade ");
        int grade = input.nextInt();
        input.close();

    // line 22 
    while(x.hasNextInt()) {
        int a = x.nextInt();
        int b = x.nextInt();
        if ( a == grade) {
            System.out.println("Your new grade is: "+b);
        }
    }
}

public void closeFile() {
    x.close();
}
}
7
  • This is my .txt file 13 12 11 12 10 10 9 7 8 7 7 4 6 2 5 0 3 0 0 -3 Commented Oct 20, 2015 at 9:45
  • I suspect what's actually happened is that your x variable is null. Was there a "File not found." prompt in the console when you ran it? Had you actually called openFile() before you called readFile()? Commented Oct 20, 2015 at 9:47
  • did you call openFile before readFile? Commented Oct 20, 2015 at 9:49
  • Usually "NullPointerExeption" means that something is not initialized, try to debug it Commented Oct 20, 2015 at 9:52
  • This is the order of my main. ReadFile r = new ReadFile(); r.openFile(); r.readFile(); r.closeFile(); Commented Oct 20, 2015 at 9:52

3 Answers 3

2

After first int a = x.nextInt() calls another int b = x.nextInt();, it may happend that x Scanner has no more data to return. You need to check x.hasNextInt() before each call of x.nextInt() method.

Sign up to request clarification or add additional context in comments.

Comments

0

i guess it is because you havnt specified the path of your file

if you are uding a file from your pc/latop's harddisk please give the complete path

x = new Scanner (new File("C://newfolder//gradeconverter.txt"));// just an example, add the actual path of you files location

2 Comments

It worked! thanks a lot for all the help every1 :) now.. is there a way to set default pathing? So if i save the file under my workspace i would not have to add the path each time
@Tacolips you can take an global variable where you add the path of your external directory(or workspace) and the n give that variable each and every place
0

Can u simply use the java 8 "way"

Files.lines(Paths.get(FILE_PATH)).forEach(System.out::print);

PS: You have forget to open the Scanner -> x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.