|
-
October 1st, 2011, 08:46 PM
#16
Re: Java tutorial *help*
 Originally Posted by Norm
You should read into a String variable first, check the contents of the String for null and then compare the contents
I'm sorry I don't understand what you mean? Do you mean like a String literal, and then compare the String literal to the contents of the file?
Like:
Code:
String check = "NewObject";
if(br.readLine.equals(check)) {// code }
-
October 1st, 2011, 09:03 PM
#17
Re: Java tutorial *help*
Read into a variable:
String theLine = br.readLine();
Norm
-
October 2nd, 2011, 01:25 AM
#18
Re: Java tutorial *help*
New and Improved:
Code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.Console;
import java.util.Arrays;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.io.BufferedInputStream;
public class Test {
public static boolean rightPlace() {
try {
String line;
BufferedReader b7 = new BufferedReader(new FileReader("Test2.txt"));
line = b7.readLine();
if(line.equals("NewObjects")) {
return true;
} else { return false; }
} catch(FileNotFoundException fnf) { System.err.println("fnf exception " + fnf.getMessage());
} catch(IOException ioe) { System.err.println("error@:"+ ioe.getStackTrace());
}
return false;
}
public static void neoWriter(String data) {
try {
InputStream i7 = new BufferedInputStream(new FileInputStream("Test2.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("Test2.txt", true));
if(rightPlace()) {
bw.newLine();
bw.write(data);
} else if(i7.read() >= i7.available()) {
System.err.println("Reached End of File");
} else { bw.newLine(); }
} catch(IOException g) { System.err.println("ERR:" + g.getStackTrace() + "\n" + g.getMessage());
}
}
public static void save(String data) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("Test2.txt", true));
neoWriter(data);
bw.newLine();
bw.flush();
} catch(IOException e) { System.err.println("err:"+ e.getMessage());
}
}
public static void main(String[] args) {
String UInput;
System.out.println("Type:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bw = new BufferedReader(isr);
try {
UInput = bw.readLine();
save(UInput);
}catch(IOException x) { System.err.println("main method err:"+ x.getStackTrace());
}
}}
I'd like to say before I continue I really appriciate your guy's help in making my code more legiable, and going through with me the proper syntax even if it isn't detrimental, but just to keep clean, and bug free code.
Anyways I still get this error
Code:
Type:
Text I want implimented into the Text.txt
Exception in thread "main" java.lang.NullPointerException
at Test.rightPlace(Test.java:30)
at Test.neoWriter(Test.java:43)
at Test.save(Test.java:56)
at Test.main(Test.java:72)
Press any key to continue . . .
Just so you don't think I'm lazy while I await one of your responses I am trying my best to track down why this runtime exception is being thrown. Keep in mind I plan on updating the exception handling as soon as I can track down ths error.
Thanks for taking the time.
-
October 2nd, 2011, 06:57 AM
#19
Re: Java tutorial *help*
java.lang.NullPointerException
at Test.rightPlace(Test.java:30)
You have seen this type of error before. YOU should know by now how to solve it. see post#15
Look at line 30 and see what variable has a null value. Then backtrack in your code to see why that variable does not have a valid value.
You forgot to do the second step in my earlier recommendation:
FIRST: You should read into a String variable first,
SECOND: check the contents of the String for null
THIRD: and then compare the contents
Last edited by Norm; October 2nd, 2011 at 06:59 AM.
Norm
-
October 2nd, 2011, 05:39 PM
#20
Re: Java tutorial *help*
Okay thanks, and I'll try to pay more attention to what your saying. Here is what I got.
Code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.Console;
import java.util.Arrays;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.io.BufferedInputStream;
public class Test {
public static boolean rightPlace() {
try {
String line;
BufferedReader b7 = new BufferedReader(new FileReader("Test2.txt"));
line = b7.readLine();
if(line != null && line.equals("NewObjects")) {
return true;
} else { return false; }
} catch(FileNotFoundException fnf) { System.err.println("fnf exception " +
fnf.getMessage());
} catch(IOException ioe) { System.err.println("error@:"+ ioe.getStackTrace());
}
return false;
}
public static void neoWriter(String data) {
try {
InputStream i7 = new BufferedInputStream(new FileInputStream("Test2.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("Test2.txt", true));
if(rightPlace()) {
bw.newLine();
bw.write(data);
} else if(i7.read() >= i7.available()) {
System.err.println("Reached End of File");
} else if(!rightPlace()) {
bw.newLine();
return;
}
} catch(IOException g) { System.err.println("ERR:" + g.getStackTrace() + "\n" +
g.getMessage());
}
}
public static void save(String data) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("Test2.txt", true));
neoWriter(data);
bw.newLine();
bw.flush();
} catch(IOException e) { System.err.println("err:"+ e.getMessage());
}
}
public static void main(String[] args) {
String UInput;
System.out.println("Type:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bw = new BufferedReader(isr);
try {
UInput = bw.readLine();
save(UInput);
return;
}catch(IOException x) { System.err.println("main method err:"+ x.getStackTrace());
}
}}
It doesn't error at runtime it just does this
Code:
Type:
What I want to write
Press any key to continue . . .
Now it just does this, but does'nt write in the Test2.txt file.
I'll figure this one out for myself thanks though guy's you taught me a lot, just thought you deserved to know I fixed that error. Edit: we fixed that error ....more so you guys than me ; )
Last edited by kolt007; October 2nd, 2011 at 05:56 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|