Results 1 to 6 of 6

Thread: Null pointer error in java

  1. #1
    Join Date
    Mar 2010
    Posts
    197

    Null pointer error in java

    Hello,
    This is my code
    Code:
    c1 { int h, double b}
    c2 { int lb double[] y, c1[][] x}
    
    class3{
    
    myObject = new c2();
    
    myObject.lb = Nb_int;
    myObject.y = new double[nb_int];
    myObject.x = new c1[nb_int][nb_int];
    }
    I get an error "null pointer", the error lies in assigning values to my myObject.x, I think a pb of initialization, I'm lost in the objects included in other objects. Any help regarding this will be appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Null pointer error in java

    Hello,
    The init an array has more dimensions this fact sequentially
    Have a look at the following code
    Code:
        Object t[][] = new Object[10][];
        for (int i = 0; i < 10; i + +)
        {
          t[i] = new Object[10];
        }
    Hope this will help you.

  3. #3
    Join Date
    Dec 2009
    Posts
    202

    Re: Null pointer error in java

    Hello,
    Even I am trying a similar kind of a program. I find it hard to understand. Your code will not create but initializes. Am I mistaken? I tired to assign values to myObject.x [0] [0]. MyObject.x a and [0] [0]. , I have the error "null pointer"? I have taken the reference of the above post as a example. If you have any other alternative for this then please let me know. Thanks in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    359

    Re: Null pointer error in java

    Hello,
    The above code sets the table has 2 dims so it can receive Object (it was just an example, use the type that you agree) but do not fill with objects. So if you access the element array [0] [0] is null that obviously has you fill your table after its init. I hope you are getting the point what I am trying to explain here. If you need more information on this then you should turn to the core java for the basics.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Null pointer error in java

    Hello,
    I have a code with me, please check if this helps
    Code:
    c1 { int h, double b}
    c2 { int lb double[] y, c1[][] a}
    
    class3{
    
    obj = new c2();
    
    obj.lb = Nb_int;
    obj.y = new double[nb];
    c1[][] a;
      a = new c1[size1][];
      for(int i = 0; I <a.length; I + +) {
          a[i] = new c1[size2];
          for(int j = 0J <a[i].lengthJ + +){
            a[i][j] = new c1(-1, -1);
          }
    }

  6. #6
    Join Date
    Mar 2010
    Posts
    197

    Re: Null pointer error in java

    Hello,
    Please check my updated code:
    Code:
    c1 { int h, double b}
    c2 { int l double[] y, c1[][] a}
    
    class3{
    
    obj = new c2();
    
    obj.l = Nb_int;
    obj.y = new double[nb];
    c1[][] a;
      a = new c1[size1][];
      for(int i = 0; I <a.length; I + +) {
          a[i] = new c1[size2];
          for(int j = 0J <a[i].lengthJ + +){
            a[i][j].Index = -1;
          a[i][j].value = -1;
          }
    }
    / / Finally, I must have:
    obj.l = val; / / Successful
    obj.y = aut; / / Successful
    obj.a[i][j].h = une; / / Null pointer
    obj.a[i][j].b = encr; / / Null pointer

Similar Threads

  1. Replies: 3
    Last Post: 04-02-2012, 12:58 AM
  2. Dereferencing NULL pointer in C++
    By TO-Phir in forum Software Development
    Replies: 4
    Last Post: 26-06-2011, 07:42 PM
  3. Nokia 6300 gives "Null Pointer" error message
    By Nadiaa in forum Portable Devices
    Replies: 4
    Last Post: 08-02-2010, 09:49 PM
  4. Differentiation between void pointer and null pointer
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 11:11 AM
  5. java error “javascript:void(null)”
    By Kitaen in forum Windows XP Support
    Replies: 3
    Last Post: 27-07-2007, 05:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •