why does the toCharArray throw nullpointer?
Code:String allTxt;
allTxt = br.readLine();
char[] araFull = allTxt.toCharArray();
Printable View
why does the toCharArray throw nullpointer?
Code:String allTxt;
allTxt = br.readLine();
char[] araFull = allTxt.toCharArray();
Because allTxt is null. Presumably because readline() is returning null.
Always read the API docs so you know what a method does and what values it can return. I'm guessing br is a BufferredReader in which case the API docs say "Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached"
If there is a chance that you might get null pointer then please use use try catch block , in this case it is a null pointer exception.
Generally speaking you should not catch NullPointerException as they are normally thrown because of a coding error ie you are trying to use a reference which you believe is valid but it isn't.Quote:
If there is a chance that you might get null pointer then please use use try catch block , in this case it is a null pointer exception.
If a variable may be null (eg because, as in this case, the method can return null) you should explicitly test for it not being null before you attempt to use it.
I was going to say that exact thing keang +D. Just because, the board instructions call for me to do so.
my solution:
Code:public static void readOutput() throws IOException{
String allTxt;
int cap;
int read;
try
(BufferedReader br = new BufferedReader(new FileReader(file))) {
allTxt = br.readLine();
cap = allTxt.length();
if(allTxt != null) {
CharArrayWriter caw = new CharArrayWriter(cap);
caw.write(allTxt, 0, cap);
char[] araFull = caw.toCharArray();
// code continues
Do you really think you've solved the problem with this code?Code:allTxt = br.readLine();
cap = allTxt.length();
if(allTxt != null) {...
What's going to happen when the second line runs if allTxt is null?
I am missunderstanding unless that if statment returns true it won't run at all?
But that line of code is before the if statement :rolleyes:
Those variables are not instantiated yet. They are able to be assinged null?
Is a null string anyway isn't it? Because, it has no instantiated value/variable?Code:String s;
:cry: No I mean the line:Quote:
Those variables are not instantiated yet.
It's before the if statement that checks alltxt is not null.Code:cap = allTxt.length();
Excuse my ignorence if I'm wrong but, I thought that 'cap' was considedered null untill assinged to a variable. Sense I am assigning it to a possible null if it's null the cap vairable will not throw an exception. So in my head it will work like this..
Code:try
(BufferedReader br = new BufferedReader(new FileReader(file))) {
allTxt = br.readLine();
cap = allTxt.length(); // at this point both allTxt variable and, cap are considered either
// null or, the integer value of length()/readLine()
if(allTxt != null) { // test's to see if allTxt is null and, if it is the below refrences will not be
// executed, causing no null pointer to be thrown.
CharArrayWriter caw = new CharArrayWriter(cap);
caw.write(allTxt, 0, cap);
char[] araFull = caw.toCharArray();
:confused: Why do you think this has anything to do with cap?Quote:
Excuse my ignorence if I'm wrong but, I thought that 'cap' was considedered null untill assinged to a variable. Sense I am assigning it to a possible null if it's null the cap vairable will not throw an exception. So in my head it will work like this..
cap is presumably an int, ie a primitive type, and so you can't assign null to it anyway, but that aside the issue is not with cap it's with allTxt.
Read what you have written as a comment - how can it make any sense to attempt to call a method on allTxt when it might be null. If allTxt is null there is no object to call the method on and so you get a NullPointerException.Quote:
cap = allTxt.length(); // at this point both allTxt variable and, cap are considered either
// null or, the integer value of length()/readLine()
You assign:
followed byCode:allTxt = br.readLine();
without checking if allTxt is null.Code:cap = allTxt.length();
If br.readLine() returns null, allTxt is null.
If allTxt is null, you get a null pointer exception thrown.
It is thrown because you are trying to use a reference (allTxt) which is pointing to nothing (null), not the assignment of cap.
Ah, yes I see where my mistake is. Null can be anything, but nothing can be null..
You can't check if br.readLine() is null, you can only check if br is null or if br.readLine() returns null.Quote:
Wouldn't I want to check if br.readLine() is null?
You don't need to check if br is null because you have created a new BufferedReader object and assigned it to br - ie br can't possibly be null.
The value return from br.readLine() may be a String or it maybe null. You assign the returned value to allTxt ie allTxt may reference a String or it may be null so you need to test allTxt to see if it is null or not.
My missunderstading I forgot, null can be anything, but nothing can be null.