-
October 31st, 2011, 05:19 PM
#1
[RESOLVED] NullPointer/warnings
why does the toCharArray throw nullpointer?
Code:
String allTxt;
allTxt = br.readLine();
char[] araFull = allTxt.toCharArray();
Last edited by kolt007; October 31st, 2011 at 05:51 PM.
-
November 1st, 2011, 06:48 AM
#2
Re: NullPointer/warnings
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"
-
November 1st, 2011, 08:33 AM
#3
Re: NullPointer/warnings
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.
-
November 1st, 2011, 08:44 AM
#4
Re: NullPointer/warnings
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.
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.
-
November 1st, 2011, 01:10 PM
#5
Re: NullPointer/warnings
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
Last edited by kolt007; November 1st, 2011 at 01:14 PM.
-
November 1st, 2011, 01:46 PM
#6
Re: NullPointer/warnings
Code:
allTxt = br.readLine();
cap = allTxt.length();
if(allTxt != null) {...
Do you really think you've solved the problem with this code?
What's going to happen when the second line runs if allTxt is null?
-
November 1st, 2011, 02:14 PM
#7
Re: NullPointer/warnings
I am missunderstanding unless that if statment returns true it won't run at all?
-
November 1st, 2011, 04:31 PM
#8
Re: NullPointer/warnings
But that line of code is before the if statement
-
November 1st, 2011, 05:39 PM
#9
Re: NullPointer/warnings
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?
-
November 1st, 2011, 05:43 PM
#10
Re: NullPointer/warnings
Those variables are not instantiated yet.
No I mean the line:
Code:
cap = allTxt.length();
It's before the if statement that checks alltxt is not null.
-
November 1st, 2011, 06:23 PM
#11
Re: NullPointer/warnings
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();
-
November 2nd, 2011, 04:58 AM
#12
Re: NullPointer/warnings
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..
Why do you think this has anything to do with cap?
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.
cap = allTxt.length(); // at this point both allTxt variable and, cap are considered either
// null or, the integer value of length()/readLine()
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.
-
November 2nd, 2011, 05:16 AM
#13
Re: NullPointer/warnings
You assign:
Code:
allTxt = br.readLine();
followed by
Code:
cap = allTxt.length();
without checking if allTxt is null.
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.
-
November 2nd, 2011, 11:17 AM
#14
Re: NullPointer/warnings
Ah, yes I see where my mistake is. Null can be anything, but nothing can be null..
-
November 2nd, 2011, 11:21 AM
#15
Re: NullPointer/warnings
Originally Posted by AlexVV
You assign:
Code:
allTxt = br.readLine();
followed by
Code:
cap = allTxt.length();
without checking if allTxt is null.
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.
Wouldn't I want to check if br.readLine() is null?
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
|