0

This is a login form for a slot machine game which will evoke a "NextPage" object. I don't think this LoginDemo.java code has to do anything with the error but I prefer to post the entire code. Thanks in advance..

    //LoginDemo.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Login extends JFrame implements ActionListener
{
     JButton SUBMIT;
     JPanel panel;
     JLabel label1,label2;
     final JTextField  text1,text2;
    Login()
    {
        label1 = new JLabel();
        label1.setText("Username:");
        label1.setBounds(50,50,100,30);
        text1 = new JTextField(15);
        text1.setBounds(250,50,100,30);
        label2 = new JLabel();
        label2.setText("Password:");
        label2.setBounds(50,250,100,30);
        text2 = new JPasswordField(15);
        text2.setBounds(250,250,100,30);
        //this.setLayout(new BorderLayout());

        SUBMIT=new JButton("SUBMIT");
        SUBMIT.setBounds(350,350,100,30);

         //     panel=new JPanel(new GridLayout(3,1));
            panel=new JPanel(null);
        panel.setBounds(50,50,1000,1000);
        //  panel.setLayout(new FlowLayout());
        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);
        panel.add(SUBMIT);
            add(panel,BorderLayout.CENTER);
                SUBMIT.addActionListener(this);
                setTitle("LOGIN FORM");
    }
    public void actionPerformed(ActionEvent ae)
    {
        String value1=text1.getText();
        String value2=text2.getText();
        if (value1.equals("a") && value2.equals("a")) 
        {
            NextPage page=new NextPage();
            page.setVisible(true);
            //JLabel label = new JLabel("Welcome:"+value1);
                // page.getContentPane().add(label);
            setVisible(false);
        }
        else
        {
            //System.out.println("enter the valid username and password");
            JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
        }
    }
}   
class LoginDemo
{
    public static void main(String arg[])
{
    try
    {
        Login frame=new Login();
        frame.setSize(1000,1000);
        frame.setVisible(true);
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }

    }
}



    //NextPage.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random; 
import java.sql.*;
import java.lang.*;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class NextPage extends JFrame implements Runnable, ActionListener
{
        BufferedImage  img1,i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,star1;
    JFrame f1;
    Thread t1,t2,t3;
    int x1,y1,i,j,k,l,m,n,r1,r2,r3;
    JButton b1;
    Random r=new Random();
    TextField tf1;
        JPanel p1;

    NextPage()
    {
        setBounds(50,50,1000,1000);
        p1=(JPanel)(getContentPane());
        p1.setBounds(50,50,1000, 1000);
        p1.setLayout(null);
        //tf1=new TextField(20);
        //tf1.setBounds(50,50,100,30);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Welcome");

        b1=new JButton("START");

        //p1.add(tf1);  

        b1.setBounds(150,400,100,30);
        //try{
        b1.addActionListener(this);
        //}
        /*catch(Exception e)
        {
            System.out.println(""+e);
        }*/
        add(b1);
        setVisible(true);
        try
        {       
            i0=ImageIO.read(new File("tea.jpg"));   
                 i1=ImageIO.read(new File("coffee.jpg"));
             i2=ImageIO.read(new File("bhel.jpg")); 
            i3=ImageIO.read(new File("pizza.jpg")); 
             i4=ImageIO.read(new File("icecream.jpg")); 
             i5=ImageIO.read(new File("dosa.jpg")); 
             i6=ImageIO.read(new File("idli.jpg")); 
             i7=ImageIO.read(new File("panipuri.jpg")); 
             i8=ImageIO.read(new File("orange.jpg"));   
             i9=ImageIO.read(new File("watermelon.jpg"));   
             i10=ImageIO.read(new File("pavbhaji.jpg"));    
             i11=ImageIO.read(new File("noodles.jpg")); 
             i12=ImageIO.read(new File("sandwich.jpg"));    
         }
        catch (IOException ie) 
        {
            System.out.println("Error:"+ie.getMessage());   
        }

        j=0; m=0; n=0;

        r1= r.nextInt(13);
        r2= r.nextInt(13);
        r3= r.nextInt(13);

    }
    public void init()
    {
        t1=new Thread(this);
        t2=new Thread(this);
        t3=new Thread(this);
    }   

    public void start()
    {
        t2.start();
        t3.start();
    }
    public void run()
    {
        try
        {
            if(Thread.currentThread()==t1)
            for(j=0; j<150; j++)
            //for(i=0; i<=150; i+=20)
            {   
                r1= r.nextInt(13);
                repaint();  
                t1.sleep(10);
            }

            if(Thread.currentThread()==t2)
            for(m=0; m<160; m++)
            //for(k=0; k<=150; k+=20)
            {   
                r2= r.nextInt(13);
                repaint();
                t2.sleep(10);
            }
            if(Thread.currentThread()==t3)
            for(n=0; n<170; n++)
            //for(l=0; l<=150; l+=20)
            {   
                r3= r.nextInt(13);
                repaint();
                t3.sleep(10);
            }
        }
        catch(Exception e)
        {
            System.out.println(""+e);
        }

    }

    public void paint(Graphics g)
    {
        g.setColor(Color.blue);

        g.drawRect(50,300,150,80);
        g.drawRect(200,300,150,80);
        g.drawRect(350,300,150,80);

            switch(r1)
            {
                case 0: g.drawImage(i0,65,300,this);
                break;
                case 1: g.drawImage(i1,65,300,this);
                break;
                case 2: g.drawImage(i2,65,300,this);
                break;
                case 3: g.drawImage(i3,65,300,this);
                break;
                case 4: g.drawImage(i4,65,300,this);
                break;
                case 5: g.drawImage(i5,65,300,this);
                break;
                case 6: g.drawImage(i6,65,300,this);
                break;
                case 7: g.drawImage(i7,65,300,this);
                break;
                case 8: g.drawImage(i8,65,300,this);
                break;
                case 9: g.drawImage(i9,65,300,this);
                break;
                case 10: g.drawImage(i10,65,300,this);
                break;
                case 11: g.drawImage(i11,65,300,this);
                break;
                case 12: g.drawImage(i12,65,300,this);
                break;
            }   

            switch(r2)
            {
                case 0: g.drawImage(i0,215,300,this);
                break;
                case 1: g.drawImage(i1,215,300,this);
                break;
                case 2: g.drawImage(i2,215,300,this);
                break;
                case 3: g.drawImage(i3,215,300,this);
                break;
                case 4: g.drawImage(i4,215,300,this);
                break;
                case 5: g.drawImage(i5,215,300,this);
                break;
                case 6: g.drawImage(i6,215,300,this);
                break;
                case 7: g.drawImage(i7,215,300,this);
                break;
                case 8: g.drawImage(i8,215,300,this);
                break;
                case 9: g.drawImage(i9,215,300,this);
                break;
                case 10: g.drawImage(i10,215,300,this);
                break;
                case 11: g.drawImage(i11,215,300,this);
                break;
                case 12: g.drawImage(i12,215,300,this);
                break;
            }

            switch(r3)
            {
                case 0: g.drawImage(i0,365,300,this);
                break;
                case 1: g.drawImage(i1,365,300,this);
                break;
                case 2: g.drawImage(i2,365,300,this);
                break;
                case 3: g.drawImage(i3,365,300,this);
                break;
                case 4: g.drawImage(i4,365,300,this);
                break;
                case 5: g.drawImage(i5,365,300,this);
                break;
                case 6: g.drawImage(i6,365,300,this);
                break;
                case 7: g.drawImage(i7,365,300,this);
                break;
                case 8: g.drawImage(i8,365,300,this);
                break;
                case 9: g.drawImage(i9,365,300,this);
                break;
                case 10: g.drawImage(i10,365,300,this);
                break;
                case 11: g.drawImage(i11,365,300,this);
                break;
                case 12: g.drawImage(i12,365,300,this);
                break;
            }
        }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==b1)
            t1.start();
        }

        /*public static void main(String args[])
        {
            NextPage o=new NextPage();
        }*/
    }   
} 

Here are the exceptions that I get-

F:\java\jdk\bin>java LoginDemo Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at NextPage.actionPerformed(NextPage.java:235) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) atjavax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6289) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6054) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4652) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4482) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4482) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644) at java.awt.EventQueue.access$000(EventQueue.java:85)

at java.awt.EventQueue$1.run(EventQueue.java:603)

at java.awt.EventQueue$1.run(EventQueue.java:601)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87

at

java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98

at java.awt.EventQueue$2.run(EventQueue.java:617)

at java.awt.EventQueue$2.run(EventQueue.java:615)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87

at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Can't get any solution.. I am a learner..please help..

4

2 回答 2

0

您不调用该init方法,因此当您输入该方法时您的t1变量仍然存在。nullactionPerformed

这在堆栈跟踪中相当明显(只有一件事可以null在该行上),如果从堆栈跟踪中不明显,调试器会立即显示这一点。比用数百行不相关代码创建帖子要快得多

于 2012-04-04T15:35:11.770 回答
0

您使用该变量t1来启动应该分配给该变量的线程。然而init(),创建分配给该变量的线程实例的函数永远不会被调用。存在t1并且null调用null对象上的任何函数都会导致NullPointerException.

只需调用该init()函数。我认为最初的想法是在构造函数中调用它,而您只是忘记插入它。

对于未来的问题,请考虑仅在 StackTrace 报告错误的位置附近发布行。要找到感兴趣的点,请查看 StackTrace 并在您编写的类中查找条目。在这种情况下NextPage.actionPerformed(NextPage.java:235)是一个兴趣点。在您的问题中最好标记这些点以快速获得良好的解决方案。

于 2012-04-04T15:46:37.597 回答