473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 NullPointerExce ption and I can not figure this out. Could someone
please help. Thanks.

Here's the code:

public class A extends java.applet.App let
{
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.prin tln (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.prin tln ("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 6394
"gtz669" <gt****@hotmail .com> wrote in message
news:5a******** *************** ***@posting.goo gle.com...
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 NullPointerExce ption and I can not figure this out. Could someone
please help. Thanks.

Here's the code:

public class A extends java.applet.App let
{
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.prin tln (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 NullPointerExce ption. 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 NullPointerExce ption. 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.goo gle.com...
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 NullPointerExce ption. 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.eu net.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
57869
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 were going to look :) . Here is my current dilemma: I am having a strange problem with my ASP.NET application. I am building the application using...
5
24678
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
6
2748
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 depending on the row, this value may be null.
0
3386
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: create table my_table (id int not null primary key auto_increment,
3
15289
by: needin4mation | last post by:
In this code: protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView rowView = (DataRowView)e.Row.DataItem; // Retrieve the value for the current row.
27
3102
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 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
1
1722
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 is an implementation of Linked List class for any item "E" (that also implements IMatchable for itself). It defines "LinkedList" as a recursive...
11
3233
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 my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed...
1
3186
pbala
by: pbala | last post by:
ConnectionString = "....." QueryString = "SELECT * FROM SomeTable" Dim myConnection As New MySql.Data.MySqlClient.MySqlConnection(ConnectionString) Dim myCommand As New MySql.Data.MySqlClient.MySqlCommand(QueryString, myConnection) Dim myReader As MySqlDataReader Try myConnection.Open() myReader = myCommand.ExecuteReader
0
7542
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7736
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7982
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.