0

I am trying to use Netbeans GUI Builder to rotate a line upon a key press. The code is simplified but I will answer any questions.

I have gotten the actual rotation to work separately but putting it together with the buttonpressed gives me the following error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at exam2.Exam2.jButton3MouseClicked(Exam2.java:372)
    at exam2.Exam2.access$200(Exam2.java:18)
    at exam2.Exam2$3.mouseClicked(Exam2.java:222)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270

myMouseClicked event which reads whatever the new value in the jtextfield is and saves it in newhour.

//function of JFrame  
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
    setHour();
    Integer newHour = (Integer) currentHour +1;
    String newHourStr = newHour.toString();
    jTextField1.setText(newHourStr);
    myCanvas.updateHour(newHour);  //this is where the error occurs
}  

//function of Canvas which extends JPanel
public void updateHour (int currentHour) {
    currentHour = getHour();
    System.out.println("the hour at hand" + currentHour); //does not print this
    int currentAngle = currentHour*30;
    Stroke stroke = new BasicStroke(3f);
    g2d.setStroke(stroke);

    double lengthHour = Math.pow(Math.pow(centerX-this.getWidth()/2,2)+Math.pow(centerY-(this.getHeight()/2)-radius,2),0.5);

    int xHourChange = (int) (lengthHour * Math.cos(Math.toRadians(currentAngle)));
    int yHourChange = (int) (lengthHour * Math.sin(Math.toRadians(currentAngle)));
    g2d.drawLine(centerX, centerY, centerX + xHourChange, centerY - yHourChange);
}


//function of Canvas which extends JPanel
public void paintComponent(Graphics g){
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    g.setColor(Color.BLACK);
    drawCircle(g);
    drawFace(g);
    drawHour(g);  //this works perfectly
    //drawMinute(g);
    //drawSecond(g);
}
martin
  • 3,149
  • 1
  • 24
  • 35
Sana
  • 73
  • 1
  • 1
  • 5
  • 1
    1: Don't use a `MouseListener` for buttons, use an `ActionListener`, a use may trigger the button in otherways then using the mouse; 2: Call `super.paintComponent` within your `paintComponent` method to ensure you're not breaking the current paint chain; 3: Provide a runnable example which demonstrates your problem and which doesn't mean we need to play the "guess what's wrong with my code" game – MadProgrammer Jun 05 '15 at 06:58
  • What is myCanvas? Likely you haven't initialized it. – grey00 Jun 05 '15 at 07:00
  • I've tried both action and mouselistener. They give the same error. As for runnable code it's pretty hard since I have so many swing components and Netbeans automatically generated a lot of code as well. Could I message it? Of course I have initialized myCanvas... – Sana Jun 05 '15 at 07:07
  • "Guess" - `newHour` is `null`, `jTextField1` is `null`, `myCanvas` is `null` – MadProgrammer Jun 05 '15 at 07:13
  • Without a runnable example it is simply impossible for us to know what the cause of the problem is, you will need to debug the situation yourself, using a combination of `System.out.println` statements and the dugger – MadProgrammer Jun 05 '15 at 07:15

0 Answers0