1

I'm completely new to java. I'm trying to write a simple java program that takes the input for coordinates and height,width for a rectangle and draws it. But i'm getting NullPointerException error. Here is my code:

import javax.swing.JFrame;
import java.util.Scanner;
import java.awt.*;
public class shape extends JFrame{
public int x,y,width, height;
public void setxyhw(int f, int g, int h, int i){
  this.x = f;
  this.y = g;
  this.width =h;
  this.height = i;
 }

public shape(){
setTitle("frame1");
setSize(960,960);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g){

g.drawRect(x,y,height,width);

}


public static void main(String[] args) {
    int a,b,c,d ;
    Scanner in = new Scanner(System.in);
    System.out.println("Input Co-ordinates and widh & height for rectangle");
    a= in.nextInt();
    b=in.nextInt();
    c=in.nextInt();
    d=in.nextInt();

    shape s=new shape();

    s.setxyhw(a,b,c,d);

    s.paint(null);
}

}
2
  • 1
    If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately neither of which you've posted here with your question. Please fix this so that we can help you. Commented Mar 7, 2015 at 22:05
  • OK. Here's the message i got "Exception in thread "main" java.lang.NullPointerException at shape.paint(shape.java:21) at shape.main(shape.java:38)" Commented Mar 7, 2015 at 22:12

2 Answers 2

4

Yikes:

s.paint(null);

You're calling paint directly, passing in a null parameter, and then wondering why the method throws a NullPointerException!

Solution:

  • Don't do this. Never call paint directly. That's for the JVM to do.
  • Draw in a JPanel's paintComponent method, not directly in the JFrame. Drawing directly in the JFrame can cause unwanted side effects in the drawing of borders and child components, and won't animate smoothly later when you try to do animation.
  • Call repaint() when you want to repaint your GUI.
  • Read the Swing graphics tutorials. You can find links to the Swing tutorials and other Swing resources here: Swing Info.
Sign up to request clarification or add additional context in comments.

3 Comments

why should he not call paint directly on his own class? Its shape s=new shape(); s.paint() we're talking about.
@Alexander: because where would he get a viable Graphics object? The Graphics object should be given to you from the JVM, in the paintComponent method. The only exception I can think of is if you want to draw a component onto a BufferedImage, and then you'd use the Graphics object obtained from the BufferedImage to do this.
@alexander paint is a public defined by the class hierarchy (Defined by Window) to honor the requirements of the paint subsystem, a system which doesn't involve user or developer interaction. The method will be called automatically by the paint subsystem when it wants something painted. In general practice, you should never need to call it manually
2

You have null as Parameter at s.paint(null); and call g.drawRect(x,y,height,width); on These object. Parameter for this paint method must be a Graphics object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.