By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Null exception assigning string

P: n/a
I have a class which I am using for data stroage. I declare an
instance of that class in my main class which is running my java
applet. I Iassign it a value in the init () function and it works
fine but when I try to do the same assignment later on, it restults in
a NullPointerException and I can not figure this out. Could someone
please help. Thanks.

Here's the code:

public class A extends java.applet.Applet
{
TData tinfo [];
public void init()
{
//This will print out fine with no exception
tinfo = new TData[3];
tinfo [0] = new TData();
tinfo [1] = new TData();
tinfo [2] = new TData();
tinfo [0].localt = "BS";
System.out.println (tinfo[0].localt);
}
public void processdata ()
{
//gets the string the user has entered
String str = userinf.getText ();
//results in the exception
try {
tinfo[0].local_ticker = "BS";
}
catch (Exception e){ System.out.println ("SET" + e);}
}
}

//TData class from another file:
public class TData
{
String localt;
//added an initalizeation method but did nothing .. shouldn't need
one since this modifying a String
}

Any Suggestions ???
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"gtz669" <gt****@hotmail.com> wrote in message
news:5a**************************@posting.google.c om...
I have a class which I am using for data stroage. I declare an
instance of that class in my main class which is running my java
applet. I Iassign it a value in the init () function and it works
fine but when I try to do the same assignment later on, it restults in
a NullPointerException and I can not figure this out. Could someone
please help. Thanks.

Here's the code:

public class A extends java.applet.Applet
{
TData tinfo [];
public void init()
{
//This will print out fine with no exception
tinfo = new TData[3];
tinfo [0] = new TData();
tinfo [1] = new TData();
tinfo [2] = new TData();
tinfo [0].localt = "BS";
System.out.println (tinfo[0].localt);
}
public void processdata ()
{
//gets the string the user has entered
String str = userinf.getText ();
//results in the exception


The str variable isn't the problem. In that situation, there's no way for it
to throw a NullPointerException. Your problem is with userinf. It is null. I
don't know why because you didn't give us the relevant code.
Jul 17 '05 #2

P: n/a
> > public void processdata ()
{
//gets the string the user has entered
String str = userinf.getText ();
//results in the exception


The str variable isn't the problem. In that situation, there's no way for it
to throw a NullPointerException. Your problem is with userinf. It is null. I
don't know why because you didn't give us the relevant code.


The getText () is not the issue. I have done a println to make sure
that
what the user inputs is recived by the code and I do a length check on
the string I get but here's the relevant code:

In the init () function :
TextField userinf;
userinf = new TextField(15);
userinf.setText("");

Then when the submit button is pressed.,I call the processdata ()
function.

I know the problem is the assignment of the string "BS" but why the
same line of code behave differently I do not know.
Same line of code: tinfo[0].localt = "BS" is in
The line of code assigning string "BS" in the processdata () function
assigning is the same as the line of code assigning the string "BS" in
the init function.

For whatever reason, the init 9) function assigns the string and does
not throw an exception, but the processdata function throws the
exception when assigning the string.
Jul 17 '05 #3

P: n/a
"gtz669" <gt****@hotmail.com> wrote in message
news:5a**************************@posting.google.c om...
public void processdata ()
{
//gets the string the user has entered
String str = userinf.getText ();
//results in the exception


The str variable isn't the problem. In that situation, there's no way for it to throw a NullPointerException. Your problem is with userinf. It is null. I don't know why because you didn't give us the relevant code.


The getText () is not the issue. I have done a println to make sure
that
what the user inputs is recived by the code and I do a length check on
the string I get but here's the relevant code:

In the init () function :
TextField userinf;
userinf = new TextField(15);
userinf.setText("");

Then when the submit button is pressed.,I call the processdata ()
function.

I know the problem is the assignment of the string "BS" but why the
same line of code behave differently I do not know.
Same line of code: tinfo[0].localt = "BS" is in
The line of code assigning string "BS" in the processdata () function
assigning is the same as the line of code assigning the string "BS" in
the init function.

For whatever reason, the init 9) function assigns the string and does
not throw an exception, but the processdata function throws the
exception when assigning the string.


Oh, my bad. I was looking at the wrong line. But again you have incomplete
or incorrect code. What is local_ticker? Look here:
http://www.physci.org/codes/sscce.jsp

Then post some compilable code that produces the same problem. Odds are,
you'll find the problem yourself when you really start looking.
Jul 17 '05 #4

P: n/a
I figured it out this morning when doing what you had pointed me to.
Before assigning the string I had called a resetData function which
was setting tinfo to null. Being a C programmer, I was hacking a
quick way to clear out the contents of the class forgetting that in
java everything is really a reference.
Thanks for the tip.
Jul 17 '05 #5

P: n/a
gtz669 wrote:
I figured it out this morning when doing what you had pointed me to.
Before assigning the string I had called a resetData function which
was setting tinfo to null. Being a C programmer, I was hacking a
quick way to clear out the contents of the class forgetting that in
java everything is really a reference.
Thanks for the tip.


Had you done that in C you would have got a segfault. :)

--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.