0

This code for drawing shapes in graphic2d but when I press draw button I have an error and I don't know why I have a null pointer exception and I think the error maybe from the method "forward, right,left and move to" , I am new to java and I search in google but I don't found solution

the error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        ayaaaa
            at com.company.Main.actionPerformed(Main.java:101)
            at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
            at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
            at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
            at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
            at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:269)
            at java.desktop/java.awt.Component.processMouseEvent(Component.java:6578)
            at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3343)
            at java.desktop/java.awt.Component.processEvent(Component.java:6343)
            at java.desktop/java.awt.Container.processEvent(Container.java:2259)
            at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4961)
            at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2317)
            at java.desktop/java.awt.Component.dispatchEvent(Component.java:4793)
            at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
            at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4539)
            at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4480)
            at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2303)
            at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2758)
            at java.desktop/java.awt.Component.dispatchEvent(Component.java:4793)
            at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:766)
            at java.desktop/java.awt.EventQueue.access$500(EventQueue.java:97)
            at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:717)
            at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:711)
            at java.base/java.security.AccessController.doPrivileged(Native Method)
            at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
            at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:99)
            at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:739)
            at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:737)
            at java.base/java.security.AccessController.doPrivileged(Native Method)
            at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
            at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:736)
            at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:199)
            at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
            at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
            at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
            at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
            at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

My code :

    package com.company;
        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        public class Main  extends JFrame implements ActionListener {
            Graphics2D g2d;

            Container c;
            JLabel label_x, label_y,label_len,label_angle,label_direction,label_shape;
            JTextField text_x,text_y,text_len,text_angle,text_direction,text_shape;
            JButton draw;
            public Main(){

                c = getContentPane();
                c.setLayout(null);
                label_x = new JLabel("x");
                label_x.setBounds(20, 20, 50, 30);
                text_x = new JTextField();
                text_x.setBounds(70, 20, 50, 30);
                label_len = new JLabel("Lenght");
                label_len.setBounds(130, 20, 50, 30);
                text_len = new JTextField();
                text_len.setBounds(180, 20, 50, 30);
                label_angle = new JLabel("Angle");
                label_angle.setBounds(240, 20, 50, 30);
                text_angle = new JTextField();
                text_angle.setBounds(290, 20, 50, 30);
                label_y = new JLabel("y");
                label_y.setBounds(20, 60, 50, 30);
                text_y = new JTextField();
                text_y.setBounds(70, 60, 50, 30);

                label_direction = new JLabel("Direction");
                label_direction.setBounds(130, 60, 70, 30);
                text_direction = new JTextField();
                text_direction.setBounds(200, 60, 50, 30);
                label_shape = new JLabel("Shape to draw");
                label_shape.setBounds(260, 60, 100, 30);
                text_shape= new JTextField();
                text_shape.setBounds(360, 60, 50, 30);
                draw = new JButton("Draw");
                draw.setBounds(200, 140, 100, 50);
                draw.addActionListener(this);
                c.add(label_x);
                c. add(label_y);
                c. add(text_x);
                c. add(text_y);
                c. add(label_angle);
                c. add(label_direction);
                c. add(label_len);
                c. add(label_shape);
                c. add(draw);
                c. add(text_angle);
                c. add(text_len);
                c. add(text_shape);
                c.  add(text_direction);
                c.setBackground(Color.gray);
                setVisible(true);
                setSize(600,600);
            }
            public void drawshape(int x,int y,int len,int dir,double angle,String s){
                System.out.println("ayaaaa");
                for(int i=0;i<s.length();i++){
                    char z=s.charAt(i);
                    if(z=='F'){
                        forward(len);
                    }else if(z=='+'){
                        right(angle);
                    }else if(z=='-'){
                        left(angle);
                    }else if(z=='r'){
                        g2d.setColor(Color.red);   }
                    else if(z=='b'){
                        g2d.setColor(Color.blue);  }
                    else if(z=='g'){
                        g2d.setColor(Color.green);
                    }
                }
            }

            public void left(double a){
                g2d.rotate(-a*Math.PI/180);
            }
            public void right(double a){
                g2d.rotate(a*Math.PI/180);
            }
            public void forward(int len){
                g2d.drawLine(0,0,len,0);
                g2d.translate(len,0);
            }
            public void moveTo(int x,int y){
                g2d.translate(x,y);
            }

            public static void main(String[] args) {
        new Main();
            }
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == draw) {
                    System.out.println("ayaaaa");
                  //  g2d.drawLine(0,0,50,0);
                    drawshape(Integer.parseInt(text_x.getText()),Integer.parseInt(text_y.getText()),Integer.parseInt(text_len.getText()),
                            Integer.parseInt(text_direction.getText()),Double.parseDouble(text_angle.getText()),text_shape.getText());
                }
            }

        }
Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29
aya
  • 1
  • 1
    *I think the error maybe from the method "forward, right,left and move to"* - the error message tells you exactly where the problem is. The problem is on line 101 of your class. So figure out which variable on the line is null and then fix the problem. – camickr May 09 '20 at 14:58

1 Answers1

0

You never initialize Graphics2D g2d; so it is null. In many methods e.g. drawshape you call some method of it e.g. g2d.setColor(Color.red);. These calls produce a NullPointerException.

See What is a NullPointerException, and how do I fix it? for detailed explanation about NullPointerException.

Michael Kreutz
  • 1,216
  • 6
  • 9