0

my code is

import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class point extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                point frame = new point();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public point() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setType(Type.UTILITY);
    getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 300, 300);
    getContentPane().add(panel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(200, 200, 450, 300);
    contentPane = new JPanel();

    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    Graphics g = panel.getGraphics();

    g.drawLine(10, 10, 20, 20); 

}
}

in g.drawLine or another draw I have error but eclipse note have error in code my error is :

> java.lang.NullPointerException
    at org.eclipse.wb.swing.point.<init>(point.java:54)
    at org.eclipse.wb.swing.point$1.run(point.java:20)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

thanks for your help

I only need to draw any thing in to the panel My IDE is eclipse and using WindowsBuilder and My IDE not get error in code and I try to other Graphics Draw but have error too

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I see this post buy not any help to me – MohammadReza Ghafarpour Apr 19 '17 at 05:53
  • 1
    *"why duplicate"* - because `java.lang.NullPointerException`. The resolution of a NPE is almost always the same. You need to use the stack trace to locate the origin of the NPE and determine why it's `null`, this debugging 101 – MadProgrammer Apr 19 '17 at 06:04
  • 1
    My guess is `Graphics g = panel.getGraphics();` this is returning `null` as the component has not yet been realised on the screen. As any good tutorial will tell, this is NOT how custom painting is done, there should never be a need to call this method – MadProgrammer Apr 19 '17 at 06:07
  • @MadProgrammer how to fix it ? – MohammadReza Ghafarpour Apr 19 '17 at 06:24
  • 1
    You could start with the Offical Java Tutorials, maybe something like [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer Apr 19 '17 at 06:26
  • It looks like you might be using Netbeans, which is very helpful to get started with a simple GUI. Continue to use Netbeans to design and create the full application. The method: 'public point()' looks like your own code, but that's probably where you're getting into trouble. Let the Designer in Netbeans create the code for you. You shouldn't need to add things like: 'g.drawline()' or 'panel.getGraphics()'. This should be done automatically by Netbeans. If you're not using Netbeans, I strongly recommend it (or Eclipse or some other IDE). – KSK Apr 19 '17 at 14:26

0 Answers0