null pointer exception problem (scope problems)

Hi, I'm trying to export from a csv file. I've done it once with the destinations variable but the distance one I am doing the exact same thing as the working destinations but comes up as a null pointer exception even though i have created global variables for it.

int sWidth = 1000;
int sHeight = 600;
IButton btn1;
IImageButton btn2;
IImageButton btn3;
color rectangleCol;
boolean isTiming;
int savedTimer;
int elapsedTime;
int y;
PImage second;
PImage bg;
PImage img1;
PImage img2;
PImage img3;
PImage img4;
PImage img5;
PImage img6;
PImage img7;
PImage img8;
PImage img9;
String currentScreen = "start";
String[] IBtnImages = {
  "buttonone.jpg", "buttontwo.jpg", "stop2.jpg"
};
String[] ImageBtnImages = {
  "map.jpg", "maprollover.jpg", "mapbuttonclick.jpg", "home.jpg", "returnrollover.jpg", "returnpressed.jpg"
};
String buttonPressedID = "none";
Table table;
int checkThisNumber = elapsedTime*500;
int xCoord;
int yCoord;
String destination;
String distance;
boolean hasResult = false;
StringList destinations;
StringList distances;
IntList xCoords;
IntList yCoords;
PFont font;
PFont font2;






void setup() {

  destinations = new StringList();
  distances = new StringList();
  xCoords = new IntList();
  yCoords = new IntList();



  table = loadTable("xy2.csv", "header");

  println(table.getRowCount() + " total rows in table"); 

  for (TableRow row : table.rows()) {

    int destination = row.getInt("destination");
    int distance = row.getInt("distance");
    String x = row.getString("x");
    String y = row.getString("y");
    String elapsedtime = row.getString("elapsedtime");

  }

  img1 = loadImage(ImageBtnImages[0]);
  img2 = loadImage(ImageBtnImages[1]);
  img3 = loadImage(ImageBtnImages[2]);
  img4 = loadImage(ImageBtnImages[3]);
  img5 = loadImage(ImageBtnImages[4]);
  img6 = loadImage(ImageBtnImages[5]);

  img7 = loadImage(IBtnImages[0]);
  img8 = loadImage(IBtnImages[1]);
  img9 = loadImage(IBtnImages[2]);

  font = loadFont("Dia-40.vlw");
  font2 = loadFont("font2-14.vlw");


  size(sWidth, sHeight);

  btn1 = new IButton("button 1", 187, 360, img7, img8, img9);

  btn2 = new IImageButton("button 2", 800, 10, img1, img2, img3);
  btn3 = new IImageButton("button 3", 20, 15, img4, img5, img6);


  bg = loadImage("timepiecehomescreen.jpg");
  second = loadImage("timepiecemap.jpg");
}

void draw() {

  if (currentScreen == "start") {
    background(bg);


    btn1.update();
    btn2.update();
  } 
  else if (currentScreen == "second") {

    background(second);


    if (hasResult == true) {
      println(xCoord+" : "+yCoord);
      stroke(255, 31, 188);
      line(445, 510, xCoord, yCoord);

      textFont(font2, 14);
      text(destination, xCoord, yCoord);
      fill(211, 68, 169);

      textFont(font, 40);
      text("WELCOME TO...", 350, 50);
      fill(211, 68, 169);

      textFont(font2, 25);
      text("Distance travelled...", 380, 85);
      fill(211, 68, 169);

      textFont(font2, 25);
      text(distance, 610, 85);
      fill(211, 68, 169);

    } 
    else {
      //show record of destinations
      for (int i=0; i<destinations.size(); i++) {

        String dest = destinations.get(i);

        int xCo = xCoords.get(i);
        int yCo = yCoords.get(i);

      stroke(255, 31, 188);

        line(445, 510, xCo, yCo);

        textFont(font2, 14);
        text(dest, xCo, yCo);
        fill(211, 68, 169);

        textFont(font, 40);
        text("TRAVEL HISTORY", 350, 50);
        fill(211, 68, 169);

      }


    }

    btn3.update();
  }
}

void mousePressed() {

  if (btn1.mPressed()) {
    buttonPressedID = btn1.getID();



    if (isTiming = !isTiming) {
      savedTimer = millis();
      println("Current Time: " + savedTimer/1000 + "s.");
    }

    else {
      elapsedTime = (millis() - savedTimer) / 1000;
      println("Elapsed Time: " + elapsedTime + "s.\n");

      int checkThisNumber = elapsedTime*500;
      //boolean hasResult1 = true;    

      for (int check=checkThisNumber;check>0;check-=500) {

        TableRow result1 = table.findRow(str(check), "elapsedtime");
        println(check);

        if (result1 == null) {
          hasResult = false;
        } 
        else {
          xCoord = result1.getInt("x");
          yCoord = result1.getInt("y");
          destination = result1.getString("destination");
          hasResult = true;
          destinations.append(destination);
          xCoords.append(xCoord);
          yCoords.append(yCoord);
          break;
        }
      }

      println(hasResult);

      if (hasResult == true) {
        currentScreen = "second";
      }
    }
  }



  if (btn2.mPressed()) {
    buttonPressedID = btn2.getID();
    hasResult = false;
    currentScreen = "second";
  }

  if (btn3.mPressed()) {
    buttonPressedID = btn3.getID();

    currentScreen = "start";
  }
}


void mouseReleased() {

  btn1.mReleased(); 
  btn2.mReleased();
  btn3.mReleased();
}

Answers

  • Answer ✓

    please post the stack trace and the line number of the error as seen in the console when you run it

    (we can't run your code - we have none of the data files, images or extra classes)

  • Answer ✓

    (is there a FAQ on how to ask questions? maybe there needs to be. and a link to it from forum pages...)

  • sorry its 131!!!

  • where are you setting distance?

  • (you aren't)

  • int sWidth = 1000; int sHeight = 600;

    These duplicate the functionality of the built-in variables width and height. Just call size(1000, 600); and uses these variables. Move the call to size() to the start of setup(), as said in the reference...

    int checkThisNumber = elapsedTime*500;

    There, elapsedTime is still zero, to checkThisNumber will be zero...

    See also the article From several variables to arrays, always recommended when we see img1, img5, etc.

  • I set the distance in the global variables at the top (35) and in draw (65).

    (and as for the size thing, i realise that but our teacher prefers it to be the way we have done it)

  • distance is an int, so it cannot be null (can be zero, at worst). So I am surprised to see a NPE at this line. Are you sure that's the highlighted line when you get the exception? Can you show the stack trace, as asked?

  • Thats what i was thinking!!! Yes its definately that line highlighted. I don't know what a stack trace is but I'm assuming its the text in the black box under the code? (sorry i must sound like such an idiot!) that just says:

    40 total rows in table Current Time: 2s. Elapsed Time: 14s.

    7000 6500 true 459 : 499

    i have zero clue whats going on!!!

  • distance on line 35 is defined as a String but isn't set to anything, therefore is null.

    the distance you set on line 65 is another variable called distance, an int and local to that block of code - it's an entirely different variable because of the usual scope rules and is hiding the global String.

    in the text() it's trying to use the global version, which is still null.

  • i've figured it out! it needs to be set at 193! thanks for your help!

Sign In or Register to comment.