Hello, I'm Taco. I am trying to create a java program that will allow users to open certain files (stored in a text file) by selecting something on a JList and clicking ok. When i program an application (the only other major experience in programming is in dos batch file), I try to make sure a certain element works before continuing, starting from the begging. So, first i tried to make sure java could read from the text file items.txt and store the names and path of a file
this is the format the text in items.txt would be in (eventually i wat the user to be able press a button to create an item):
<Name> (what the user sees on the list) <filepath> (the path that will be run)
the code i used in the class is as follows:
import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; public class b extends JFrame{ JList itemList; Scanner x; String items = "items.txt"; String[] itemNames; String[] filepaths; public b(){ super("Super Essentials"); setLayout(new FlowLayout()); getItemNamesAndPaths(); } public void getItemNamesAndPaths(){ try{ x = new Scanner(new File(items)); int i = 0; while(x.hasNext()){ itemNames[i] = x.next(); filepaths[i] = x.next(); i++; } x.close(); System.out.println(itemNames); } catch(Exception e){ System.out.println("error occured"); } } }
i tried rearranging everything (except the first line) of the try block, but with everything in the try block that way it opens the window and gets rid of the exception stack, but there's still an error....
it seems to me that the error is it cant find items.txt, even if change the path and such. or it could be that it cant input anything into itemNames[] or filepaths[].
Heres the Exception stack (what was removed before i put the while loop, x.close(), and the println statement in the try block:
Exception in thread "main" java.lang.NullPointerException
at b.getItemNamesAndPaths(b.java:33)
at b.<init>(b.java:20) (where the while loop was before)
at a.main(a.java:6) (in a.java that line is b list = new b() i know fr a fact there's nothing wrong there, everything just needs to work)
By the way, I use Eclipse.