vendredi 27 février 2015

Try/Catch error exception returing null



I'm trying to read in a list of words a file using BufferedReader and determine if any are duplicates, and if they are, to not print them. I know that there is a SuperClass called "UniqueLineReader" that can check the occurrence of duplicate lines for you, and something else called Linked HashSet. However, I haven't learned either of these in class, so I'm trying not to use them.


Anyways, the e.getMessage() is printing "null", which I don't understand, because the file obviously isn't and I'm checking that it isn't. Do I have wrong placement of variables?



try {
inFile = new BufferedReader(new FileReader(inputName));
outFile = new PrintWriter(new FileWriter(outputName));

while((nextLine = inFile.readLine())!= null){

if(indexOf(nextLine) == -1){
insert(nextLine);

outFile.println(nextLine);}
else{
System.out.println(nextLine + " has been seen before!");
}
}

inFile.close();
}//try
catch(Exception e){
System.out.println(e.getMessage());
}//catch


My indexOf method:



private int indexOf(String newWord){

for(int i = 0; i < numWords; i++){
if(arrayStrings[i].equals(newWord)){
return i;
}
}

return -1;

}//indexOf


stacktrace


java.lang.NullPointerException at A2Q2.indexOf(A2Q2.java:58) at A2Q2.removeDuplicateLines(A2Q2.java:79) at TestA2Q2.main(TestA2Q2.java:10) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler. JavacCompiler.runCommand(JavacCompiler.java:272) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362) at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator .handleMethodCall(ExpressionEvaluator.java:92) atedu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84) at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77) at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47) at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246) at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Unknown Source) at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)




Aucun commentaire:

Enregistrer un commentaire