473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Null exception assigning string

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
5 6382
"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
> > 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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Herring | last post by:
Hi there: Well, let me start off by saying that I am a Visual Studio drag and drop weenie, not a real programmer. So I tend to get confused when things do not look like the instructions said they...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
6
by: Dan Keeley | last post by:
Hi, This is probably not a dotnet specific issue, however i'm using dotnet so here goes,. I'm setting a text value of a field like so: txtFax.Text = currentRow("FaxNumber") However...
0
by: axelsino | last post by:
I am using 5.0.21 community-nt and have the following in the configuration file: transaction-isolation=READ-COMMITTED sql-mode=ansi,strict_trans_tables I have defined this little table: ...
3
by: needin4mation | last post by:
In this code: protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView rowView =...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
1
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
1
pbala
by: pbala | last post by:
ConnectionString = "....." QueryString = "SELECT * FROM SomeTable" Dim myConnection As New MySql.Data.MySqlClient.MySqlConnection(ConnectionString) Dim myCommand As New...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.