我一直在控制台中收到此错误,但是当我查看它时,编码没有错误。当我不希望打开JOptionPanes时,它还会继续打开完全相同的JOptionPanes。假定循环完成后,JOptionPane重新打开,并要求用户输入Bingo卡的值。但是,在完成第一个JOptionPane循环后,它仍会填充并制作BingoCard。

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.SwingUtilities.computeIntersection(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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$1.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.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at javax.swing.JOptionPane.showOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at day1.example.BingoCard.paintComponent(BingoCard.java:22)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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$1.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.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at javax.swing.JOptionPane.showOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at day1.example.BingoCard.paintComponent(BingoCard.java:22)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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$1.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)
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at day1.example.BingoCard.paintComponent(BingoCard.java:27)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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$1.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.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at javax.swing.JOptionPane.showOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at day1.example.BingoCard.paintComponent(BingoCard.java:22)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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$1.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)


这是我的编码

package BingoPackage;


import javax.swing.JComponent;


import javax.swing.JOptionPane;

import java.awt.*;



public class BingoCard extends JComponent {


    public void paintComponent(Graphics g)
     {
        //Recover Graphics



        int BValues []= new int [5];
        int IValues []= new int [5];
        int NValues []= new int [4];
        int GValues []= new int [5];
        int OValues []= new int [5];
        JOptionPane.showMessageDialog(null," We will now guide you in making your very own Bingo Card");
        for (int B = 0; B<BValues.length; B++)
        {
        String BVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 1-15");
        BValues[B]=Integer.parseInt(BVal);
        if (BValues[B]<1 || BValues[B]>15)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int B1 = BValues [0];
        int B2 = BValues [1];
        int B3 = BValues [2];
        int B4 = BValues [3];
        int B5 = BValues [4];
        if (B1==B2 || B1==B3 || B1== B4 || B1==B5 || B2==B3 || B2== B4|| B2==B5 || B3== B4 || B3== B5 || B4==B5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int I = 0; I<IValues.length; I++)
        {
        String IVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 16-30");
        IValues[I]=Integer.parseInt(IVal);
        if (IValues[I]<16 || IValues[I]>30)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int I1 = IValues [0];
        int I2 = IValues [1];
        int I3 = IValues [2];
        int I4 = IValues [3];
        int I5 = IValues [4];
        if (I1==I2 || I1==I3 || I1== I4 || I1==I5 || I2==I3 || I2== I4|| I2==I5 || I3== I4 || I3== I5 || I4==I5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int N = 0; N<NValues.length; N++)
        {
        String NVal = JOptionPane.showInputDialog(null, "Please enter 4 different numbers"+ "\n" + "that are in the range of 31-45");
        NValues[N]=Integer.parseInt(NVal);
        if (NValues[N]<31 || NValues[N]>45)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int N1 = NValues [0];
        int N2 = NValues [1];
        int N3 = NValues [2];
        int N4 = NValues [3];
        if (N1==N2 || N1==N3 || N1== N4 || N2==N3 || N2== N4|| N3== N4)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int G = 0; G<GValues.length; G++)
        {
        String GVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 46-60");
        GValues[G]=Integer.parseInt(GVal);
        if (GValues[G]<46 || GValues[G]>60)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int G1 = GValues [0];
        int G2 = GValues [1];
        int G3 = GValues [2];
        int G4 = GValues [3];
        int G5 = GValues [4];
        if (G1==G2 || G1==G3 || G1== I4 || G1==G5 || G2==G3 || G2== G4|| G2==G5 || G3== G4 || G3== G5 || G4==G5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int O = 0; O<OValues.length; O++)
        {
        String OVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 61-75");
        OValues[O]=Integer.parseInt(OVal);
        if (OValues[O]<61 || OValues[O]>75)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int O1 = OValues [0];
        int O2 = OValues [1];
        int O3 = OValues [2];
        int O4 = OValues [3];
        int O5 = OValues [4];
        if (O1==O2 || O1==O3 || O1== O4 || O1==O5 || O2==O3 || O2== O4|| O2==O5 || O3== O4 || O3== O5 || O4==O5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }


        Graphics2D g3 = (Graphics2D) g;
        g3.setColor(Color.BLACK);
        Font bingoFont = new Font ("Tahoma", Font.PLAIN,69);
        g3.setFont(bingoFont);

        Rectangle bingosign = new Rectangle (75,10, 500, 75);
        g3.draw(bingosign);
        g3.drawString ("B   I   N  G  O", 110, 70);

        Rectangle B1Box = new Rectangle (75, 85, 100, 100);
        g3.draw(B1Box);

        Rectangle B2Box = new Rectangle (75, 185, 100, 100);
        g3.draw(B2Box);

        Rectangle B3Box = new Rectangle (75, 285, 100, 100);
        g3.draw(B3Box);

        Rectangle B4Box = new Rectangle (75, 385, 100, 100);
        g3.draw(B4Box);

        Rectangle B5Box = new Rectangle (75, 485, 100, 100);
        g3.draw(B5Box);

        Rectangle I1Box = new Rectangle (175, 85, 100, 100);
        g3.draw(I1Box);

        Rectangle I2Box = new Rectangle (175, 185, 100, 100);
        g3.draw(I2Box);

        Rectangle I3Box = new Rectangle (175, 285, 100, 100);
        g3.draw(I3Box);

        Rectangle I4Box = new Rectangle (175, 385, 100, 100);
        g3.draw(I4Box);

        Rectangle I5Box = new Rectangle (175, 485, 100, 100);
        g3.draw(I5Box);

        Rectangle N1Box = new Rectangle (275, 85, 100, 100);
        g3.draw(N1Box);

        Rectangle N2Box = new Rectangle (275, 185, 100, 100);
        g3.draw(N2Box);

        Rectangle N3Box = new Rectangle (275, 285, 100, 100);
        g3.draw(N3Box);

        Rectangle N4Box = new Rectangle (275, 385, 100, 100);
        g3.draw(N4Box);

        Rectangle N5Box = new Rectangle (275, 485, 100, 100);
        g3.draw(N5Box);

        Rectangle G1Box = new Rectangle (375, 85, 100, 100);
        g3.draw(G1Box);

        Rectangle G2Box = new Rectangle (375, 185, 100, 100);
        g3.draw(G2Box);

        Rectangle G3Box = new Rectangle (375, 285, 100, 100);
        g3.draw(G3Box);

        Rectangle G4Box = new Rectangle (375, 385, 100, 100);
        g3.draw(G4Box);

        Rectangle G5Box = new Rectangle (375, 485, 100, 100);
        g3.draw(G5Box);

        Rectangle O1Box = new Rectangle (475, 85, 100, 100);
        g3.draw(O1Box);

        Rectangle O2Box = new Rectangle (475, 185, 100, 100);
        g3.draw(O2Box);

        Rectangle O3Box = new Rectangle (475, 285, 100, 100);
        g3.draw(O3Box);

        Rectangle O4Box= new Rectangle (475, 385, 100, 100);
        g3.draw(O4Box);

        Rectangle O5Box = new Rectangle (475, 485, 100, 100);
        g3.draw(O5Box);

        g3.setColor(Color.RED);
        g3.fill(N3Box);

        g3.setColor(Color.BLACK);
        String B1String = String.valueOf(B1);
        String B2String = String.valueOf(B2);
        String B3String = String.valueOf(B3);
        String B4String = String.valueOf(B4);
        String B5String = String.valueOf(B5);
        String I1String = String.valueOf(I1);
        String I2String = String.valueOf(I2);
        String I3String = String.valueOf(I3);
        String I4String = String.valueOf(I4);
        String I5String = String.valueOf(I5);
        String N1String = String.valueOf(N1);
        String N2String = String.valueOf(N2);
        String N3String = String.valueOf(N3);
        String N4String = String.valueOf(N4);
        String G1String = String.valueOf(G1);
        String G2String = String.valueOf(G2);
        String G3String = String.valueOf(G3);
        String G4String = String.valueOf(G4);
        String G5String = String.valueOf(G5);
        String O1String = String.valueOf(O1);
        String O2String = String.valueOf(O2);
        String O3String = String.valueOf(O3);
        String O4String = String.valueOf(O4);
        String O5String = String.valueOf(O5);

        g3.drawString(B1String, 90, 160);
        g3.drawString(B2String, 90, 260);
        g3.drawString(B3String, 90, 360);
        g3.drawString(B4String, 90, 460);
        g3.drawString(B5String, 90, 560);
        g3.drawString(I1String, 190, 160);
        g3.drawString(I2String, 190, 260);
        g3.drawString(I3String, 190, 360);
        g3.drawString(I4String, 190, 460);
        g3.drawString(I5String, 190, 560);
        g3.drawString(N1String, 290, 160);
        g3.drawString(N2String, 290, 260);
        g3.drawString(N3String, 290, 460);
        g3.drawString(N4String, 290, 560);
        g3.drawString(G1String, 390, 160);
        g3.drawString(G2String, 390, 260);
        g3.drawString(G3String, 390, 360);
        g3.drawString(G4String, 390, 460);
        g3.drawString(G5String, 390, 560);
        g3.drawString(O1String, 490, 160);
        g3.drawString(O2String, 490, 260);
        g3.drawString(O3String, 490, 360);
        g3.drawString(O4String, 490, 460);
        g3.drawString(O5String, 490, 560);


    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.LIGHT_GRAY);
    Font freeSpaceFont = new Font("Helvetica", Font.PLAIN,16);
    g2.setFont(freeSpaceFont);
    g2.drawString("Free", 305, 330);
    g2.drawString("Space", 300, 350);


     }
}


怎么了?提前致谢。

最佳答案

似乎您在行上输入了空值

String BVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 1-15");


所以行

 BValues[B]=Integer.parseInt(BVal);
is causing the exception.


跟踪要获取的BVal作为输入。

重要:从用户那里获取输入时,请始终使用验证。

更新:
我进一步检查,您正在多个地方进行输入。将验证放在任何需要的地方

validation reference

10-08 01:52