0

I was given a ready code sample for an assignemt but I can't get it to work. Love when this happens!

package kth.id2010.lab.lab04;


import edu.princeton.cs.introcs.In;
import java.net.URL;

public class Driver {

    public static void main(String[] args) {
        URL url = Driver.class.getResource("kap1.txt");
        In input = new In(url);//this is where the nullPointer is pointing

        while(!input.isEmpty()){
            String line = input.readLine().trim();
            String[] words = line.split(" ");
            for (String word : words) {
                System.out.println(word);
            }
        }
    }
}

and here is the full exception that gets printed out.

Exception in thread "main" java.lang.NullPointerException

at edu.princeton.cs.introcs.In.(In.java:105)

at kth.id2010.lab.lab04.Driver.main(Driver.java:12)

I'm using maven

Necrozze
  • 61
  • 1
  • 6
  • Do you have access to the source of In? – CAG Gonzo Oct 08 '14 at 19:34
  • Can you use a debugger to check what the value of `url` is before the call to `new In(url);`? – Dawood ibn Kareem Oct 08 '14 at 19:36
  • Per the spec for Class.getResource -- `Returns: A URL object or null if no resource with this name is found` – Hot Licks Oct 08 '14 at 19:37
  • The value of url is probably null. Check if the file kap1.txt exists in your classpath. – proko Oct 08 '14 at 19:38
  • http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29 – Hot Licks Oct 08 '14 at 19:39
  • @proko the file is located in the same package as the Driver class – Necrozze Oct 08 '14 at 20:05
  • Are you using Eclipse or another IDE? – nmore Oct 08 '14 at 20:08
  • I don't have experience developing in netBeans, but it probably points your working directory to outside your files next to your source folder. Try printing your current working path: 'System.getProperty("user.dir")' – nmore Oct 08 '14 at 20:13
  • @nmore okey it's pointing to where my src folder is located, so not to the actual folder where my files are and where the .txt file is located – Necrozze Oct 08 '14 at 20:19
  • @nmore I still can't get access to it, even if I move the file there, or if I change the `Driver.class.getResource` to contain all the folders between that one and the one my classes and .txt file are in – Necrozze Oct 08 '14 at 20:34

3 Answers3

0

That's because your path is wrong and URL object is null, please be sure the kap1.txt file is under your resources path and not in another folder, also try this:

URL url = Driver.class.getResource("/kap1.txt");

If you are not using resources then refer here

Community
  • 1
  • 1
Sercan Ozdemir
  • 4,450
  • 3
  • 32
  • 59
0

The issue was that your IDE does not necessarily point your current working directory to where your java files are located. Usually it points to just inside your project folder, but can use

System.out.println(System.getProperty("user.dir"));

to print out your actual working directory. The relative paths in your code will start from here, so you want to put your kap1.txt file in whatever directory is printed by the command above.

nmore
  • 2,075
  • 1
  • 11
  • 18
  • Ok I have not used get resource in this way before. It is not a standard way to read a file. Check out http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file for more info on how to do this, though I'm not sure if you are allowed to change this code or not since you say it was given. – nmore Oct 08 '14 at 20:42
  • I'll look in to it, dont think its a problem It was more of a example code to get started – Necrozze Oct 08 '14 at 20:43
0

The problem was solved when I put the .txt file in src/main/resources folder and changed Url url = Driver.Class.getResource("kap1.txt");

to

Url url = Driver.Class.getResource("/kap1.txt");

don't know id this was what Sercan Ozdemir was refering to, but thought I should clarify how I solved it for coming users with this problem.

Necrozze
  • 61
  • 1
  • 6