Why does this give me a null pointer exception

My code is below, My apologize i do not know how to show it in code format.
why does "s2.sizewidth *= number;" return a null pointer exception. This isnt actually my project its just a sample i made to reconstruct the problem without posting 1100 lines of code

void setup() {
  noLoop();

  sizewidth = 100;
  sizeheight = 200;
  s2 = new Size2(sizewidth, sizeheight);

  number = 3;
  aproduct = new Product(number, s2);
  aproduct.multiply();
}

void draw() {
  println(s2.sizewidth);
}

int sizewidth;
int sizeheight;

class Size {
  int sizewidth;
  int sizeheight;
  Size(int w, int h) {
    w = sizewidth;
    h = sizeheight;
  }
}

Size2 s2;

class Size2 extends Size {
  Size2(int w, int h) {
    super(w, h);
  }
}

int number;

Product aproduct;

class Product {
  int number;
  Size2 s2;

  Product(int n, Size2 si2) {
    n = number;
    si2 = s2;
  }

  void multiply() {
    s2.sizewidth *= number;
    s2.sizeheight *= number;
  }
}

Answers

  • line 47 is assigning a member variable to a parameter. (line 46 also) (and 24, 25). they need to be the other way around

    number = n;
    s2 = si2;
    
  • as for formatting, there's a sticky thread at the top of the forum with the details.

    "When you have code to show, there is some ways to do it. First paste it, then select it. Then hit the C button above the text area, or hit the Ctrl+K key combination. This indents the code with four spaces or a tab, and the forum will highlight it. The code must have blank lines before and after it."

  • edited September 2014

    Highlight your code and hit CTRL+K in order to format it!
    Your inheritance above is very confusing: Why would a parent class need a reference for a subclass of itself for?
    Another 1, you don't initialize that s2 within constructor. Therefore it's still defaulted to null!

    P.S.: Due to lack of formatting, only now I've realized that Size2 s2 variable is outside anything.
    Therefore it's a sketch's "global" field!

    It's real bad programming directly access a foreign member.
    If you really wanna use sketch's s2 field within multiply(), you should pass it as a parameter for it!

  • edited September 2014

    Sorry about the formatting issues i assure you that my actual project is not as so i just threw this together quickly to recreate the issue. My bad. That makes it so it retuns no error however it does not quite achieve what i want still, which is of course my bad. is their a way to make it so that when i say:

    s2.sizewidth *= number;
    s2.sizewidth *= number;
    

    that it affects those variables within the Size2 class rather than only the Product class?

    GoToLoop: i understand the code does not make sense out of context of the legitamate project and the code with the formatting fixed is below:

    void setup() {
      noLoop();
    
      sizewidth = 100;
      sizeheight = 200;
      s2 = new Size2(sizewidth, sizeheight);
    
      number = 3;
      aproduct = new Product(number, s2);
      aproduct.multiply();
    }
    
    void draw() {
      println(aproduct.s2.sizewidth);
    }
    
    
    
    int sizewidth;
    int sizeheight;
    
    class Size {
      int sizewidth;
      int sizeheight;
      Size(int w, int h) {
        sizewidth = w;
        sizeheight = h;
      }
    }
    
    
    
    Size2 s2;
    
    class Size2 extends Size {
      Size2(int w, int h) {
        super(w, h);
      }
    }
    
    
    
    int number;
    
    Product aproduct;
    
    class Product {
      int number;
      Size2 s2;
    
      Product(int n, Size2 si2) {
        number = n;
        s2 = si2;
      }
    
      void multiply() {
        s2.sizewidth *= number;
        s2.sizeheight *= number;
      }
    }
    
  • edited September 2014

    ... that it affects those variables within the Size2 class rather than only the Product class?

    void multiply() {
      s2.sizewidth  *= number;
      s2.sizeheight *= number;
    }
    

    1st of all, class Product doesn't have those 2 fields! They belong to class Size, which Size2 inherits from.
    2nd, fields become instance variables when an object (a class instance) is created.
    3rd, since Product manipulates a Size2 object, any changes are reflected there too!
    4th, so both (Product's s2.sizewidth & s2.sizeheight) and (Size2's sizewidth & sizeheight) refer to same fields!

    void draw() {
      println(aproduct.s2.sizewidth);  // 300
      println(s2.sizewidth); // 300
    }
    
  • So what you are saying is that it already does what i wish for it to do? Yet again i apologize for the confusing format in which i recreated this in. I understand it does not make any sense to do it this way as is.

Sign In or Register to comment.