Null exception assigning string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gtz669

    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 ???
  • Ryan Stewart

    #2
    Re: Null exception assigning string

    "gtz669" <gtz669@hotmail .com> wrote in message
    news:5ae9bf03.0 402101036.39a6e 18c@posting.goo gle.com...[color=blue]
    > 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[/color]

    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.


    Comment

    • gtz669

      #3
      Re: Null exception assigning string

      > > public void processdata ()[color=blue][color=green]
      > > {
      > > //gets the string the user has entered
      > > String str = userinf.getText ();
      > > //results in the exception[/color]
      >
      > 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.[/color]

      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.

      Comment

      • Ryan Stewart

        #4
        Re: Null exception assigning string

        "gtz669" <gtz669@hotmail .com> wrote in message
        news:5ae9bf03.0 402101903.4d52d 6b6@posting.goo gle.com...[color=blue][color=green][color=darkred]
        > > > public void processdata ()
        > > > {
        > > > //gets the string the user has entered
        > > > String str = userinf.getText ();
        > > > //results in the exception[/color]
        > >
        > > The str variable isn't the problem. In that situation, there's no way[/color][/color]
        for it[color=blue][color=green]
        > > to throw a NullPointerExce ption. Your problem is with userinf. It is[/color][/color]
        null. I[color=blue][color=green]
        > > don't know why because you didn't give us the relevant code.[/color]
        >
        > 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.[/color]

        Oh, my bad. I was looking at the wrong line. But again you have incomplete
        or incorrect code. What is local_ticker? Look here:


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


        Comment

        • gtz669

          #5
          Re: Null exception assigning string

          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.

          Comment

          • chris

            #6
            Re: Null exception assigning string

            gtz669 wrote:
            [color=blue]
            > 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.[/color]

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

            --
            Chris Gray chris@kiffer.eu net.be
            /k/ Embedded Java Solutions

            Comment

            Working...