wrong format

edited June 2016 in Arduino

hey guys, new to java. only started C++ a couple of weeks ago. im having trouble trying to get a class to work, but keep getting null pointer errors. cant quite figure out what im not setting up wrong. can anyone see from the code what im doing wrong. Cheers excuse all the commented out stuff i was just trying to narrow down the problem

(Code in comments)

Tagged:

Answers

  • well thats a load of garbage ill try again

  • try again

    hey guys, new to java. only started C++ a couple of weeks ago. im having trouble trying to get a class to work, but keep getting null pointer errors. cant quite figure out what im not setting up wrong. can anyone see from the code what im doing wrong. Cheers excuse all the commented out stuff i was just trying to narrow down the problem

    import processing.serial.*;
    
    Serial port;  // Create object from Serial class
    int[] serialInArray = new int[2]; // Where we'll put what we receive
    int serialCount = 0;     // A count of how many bytes we receive
    boolean firstContact = false;
    
    //float TPS1;
    //float TPS2;
    //float inputVar = 4000.0;
    
    class Dial 
    {
      float inputVar = 4000;
      int xPos = 0;
      int yPos = 0;
      int diameter = 0;
    
      //void dialSetup(int x, int y, int d) 
      //{    
      //  xPos = x;
      //  yPos = y;
      //  diameter = d;
      //}  // end of dial setup
    
      //void dialSerial() 
      //{
      //int inByte = port.read();
      //if (firstContact == false) 
      //{
      //  if (inByte == 'A') 
      //  {
      //    port.clear();   // clear the serial port buffer
      //    firstContact = true;  // you've had first contact from the microcontroller
      //    port.write('A');  // ask for more
      //  }
      //}
      //else 
      //{
      //  serialInArray[serialCount] = inByte;
      //  serialCount++;
      //  if (serialCount > 1 ) 
      //  {
      //    //port.clear();
      //    inputVar = ((serialInArray[0] << 8) + (serialInArray[1]));
      //    //TPS2 = ((serialInArray[2] << 8) + (serialInArray[3]));
      //    //println(TPS1);
      //    //println(TPS2);
      //    port.write('A');
      //    serialCount = 0;
      //  }
      //}
      //}  //end of dial serial    
    
    
      void dialStart(int x, int y, int d) 
      {
        int xPos = x;
        int yPos = y;
        int diameter = d;
        // draw circle  
        fill(0);                                 // circle colour
        strokeWeight(1);                         // thickness of circle outline
        stroke(255);                             // colour of circle
        ellipse(xPos, yPos, diameter, diameter); // position of circle
        // draw needles
        float ratio = map(inputVar, 0, 4095, 0, PI);
        stroke(255);
        strokeWeight(4);
        line(xPos, yPos, xPos - (cos(ratio)*69), yPos - (sin(ratio)*69));
        //draw text 
        PFont fonta;
        fonta = loadFont("Latha-32.vlw");
        int roundedVar = round(inputVar);         // round var for text display
        fill(100);                                // text colour  
        textFont(fonta, 25);                       // font
        textAlign(CENTER, CENTER);                // position   
        text(100 * roundedVar / 4095, xPos, yPos + 30);
        //  Draw the minute ticks and labels 
        stroke(255, 0, 0);    
        fill(255, 0, 0);
        strokeWeight(4);
        PFont fontb;
        fontb = loadFont("Latha-32.vlw");
        for (int a = 180; a <= 360; a+=18)
        {
          float i = xPos + ( cos(radians(a)) * 90 );
          float j = yPos + ( sin(radians(a)) * 90 );
          textFont(fontb, 12);
          text(ceil((a - 180)/1.8), i, j);
        }
        for (int a = 180; a <= 360; a+=10) 
        {
          float i = xPos + ( cos(radians(a)) * 74 );
          float j = yPos + ( sin(radians(a)) * 74 );
          point(i, j);
        }
      } // // end of dialStart
    };  // end of class
    
    
    Dial dial1;
    
    //Dial dial2;
    
    void setup() {
      size(750, 250);  // Set the font and its size (in units of pixels)
      stroke(255);
      smooth();
      port = new Serial(this, "COM5", 115200); 
      //dial1.dialSerial();
      //dial1.dialSetup(125, 125, 160);
      //dial2.dialSetup(TPS2, 125, 300, 160);
    
      //fonta = loadFont("Latha-32.vlw");
    }
    
    
    void draw() {
    
      background(0);    // black background
      //dial1.dialSetup(125, 125, 160);
      dial1.dialStart(125, 125, 160);
      //dial2.dialStart();
    }
    
    
    //void serialEvent (Serial port) 
    //{
    //  int inByte = port.read();
    //  if (firstContact == false) 
    //  {
    //    if (inByte == 'A') 
    //    {
    //      port.clear();   // clear the serial port buffer
    //      firstContact = true;  // you've had first contact from the microcontroller
    //      port.write('A');  // ask for more
    //    }
    //  }
    //  else 
    //  {
    //    serialInArray[serialCount] = inByte;
    //    serialCount++;
    //    if (serialCount > 1 ) 
    //    {
    //      //port.clear();
    //      dial1.inputVar = ((serialInArray[0] << 8) + (serialInArray[1]));
    
    //      //TPS2 = ((serialInArray[2] << 8) + (serialInArray[3]));
    //      //println(TPS1);
    //      //println(TPS2);
    //      port.write('A');
    //      serialCount = 0;
    //    }
    //  }
    //}
    
  • Answer ✓

    never mind all good, just needed a coffee

    dial1 = new Dial();

    did the trick, little bit different from C++

Sign In or Register to comment.