Java getText 空指针异常

发布于 2024-12-09 16:13:28 字数 5613 浏览 1 评论 0原文

我花了大约 2 个小时试图理解为什么我的代码不断抛出运行时错误。它不想接受用户输入...我很确定我的 getext 实现是正确的。你们能看一下我的代码并帮助我看看我做错了什么吗?

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Mdt1 extends JFrame implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * @param args
 */
 JTextField twager, dcard1, dcard2, pcard1, pcard2;

public Mdt1()  {
    JFrame blackjack = new JFrame();
    Border wwager = BorderFactory.createTitledBorder("Wager");
    JPanel pwager = new JPanel();
    pwager.setBorder(wwager);
    JLabel lwager = new JLabel("Enter wager:");
    JTextField twager = new JTextField(6);
    twager.addActionListener(this);
    pwager.add(lwager);
    pwager.add(twager);

    JPanel pplayer = new JPanel();
    Border wayer = BorderFactory.createTitledBorder("Player");
    pplayer.setLayout(new BoxLayout(pplayer, BoxLayout.Y_AXIS));
    pplayer.setBorder(wayer);
    pplayer.setSize(100, 100);
    JTextField pcard1 = new JTextField(6);
    pcard1.setEditable(false);
    JTextField pcard2 = new JTextField(6);
    pcard2.setEditable(false);
    pplayer.add(pcard1);
    pplayer.add(pcard2);


    JPanel pdealer = new JPanel();
    Border wealer = BorderFactory.createTitledBorder("Dealer");
    pdealer.setLayout(new BoxLayout(pdealer, BoxLayout.Y_AXIS));
    pdealer.setBorder(wealer);
    pdealer.setSize(100, 100);
    JTextField dcard1 = new JTextField(6);
    dcard1.setEditable(false);
    JTextField dcard2 = new JTextField(6);
    dcard2.setEditable(false);
    pdealer.add(dcard1);
    pdealer.add(dcard2);

    JPanel cards = new JPanel();
    Border wards = BorderFactory.createTitledBorder("Cards");
    cards.setBorder(wards);
    cards.setSize(220, 80);
    cards.add(pplayer);
    cards.add(pdealer);

    JPanel pendgame = new JPanel();
    Border wesult = BorderFactory.createTitledBorder("Endgame");
    pendgame.setBorder(wesult);



    blackjack.setLayout(new BorderLayout());
    blackjack.setVisible(true);
    blackjack.setSize(200, 200);
    blackjack.setTitle("♣ Blackjack ♠");
    blackjack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    blackjack.add(pwager, BorderLayout.NORTH);
    blackjack.add(cards);
    blackjack.pack();
    blackjack.setResizable(false);

}

public void actionPerformed(ActionEvent e) {
    String swager = twager.getText();
    double wager;
    int  dealer1, dealer2, player1, player2;
    int  dealervalue1, dealervalue2, playervalue1, playervalue2;
    int  dealerTotal, playerTotal;


    wager = Double.parseDouble(swager);

    Random r=new Random();
    // card faces
    dealer1=r.nextInt(13) + 1;
    String sdealer1 = Integer.toString(dealer1);
    dealer2=r.nextInt(13) + 1;
    String sdealer2 = Integer.toString(dealer2);
    player1=r.nextInt(13) + 1;
    String splayer1 = Integer.toString(player1);
    player2=r.nextInt(13) + 1;
    String splayer2 = Integer.toString(player2);
    // Show dealer's card faces
    switch(dealer1)
    {
    case 1: dcard1.setText("Ace "); break;
    case 11: dcard1.setText("Jack"); break;
    case 12: dcard1.setText("Queen"); break;
    case 13: dcard1.setText("King"); break;
    default: dcard1.setText(sdealer1); break; 
    } 
    switch(dealer2)
    {
    case 1: dcard2.setText("Ace"); break;
    case 11: dcard2.setText("Jack"); break;
    case 12: dcard2.setText("Queen"); break;
    case 13: dcard2.setText("King"); break;
    default: dcard2.setText(sdealer2); break;
    } 
    // Show player's card faces
    switch(player1)
    {
    case 1: System.out.print("Ace"); break;
    case 11: System.out.print("Jack"); break;
    case 12: System.out.print("Queen"); break;
    case 13: System.out.print("King"); break;
    default: System.out.print(splayer1); break;
    } 
    switch(player2)
    {
    case 1: System.out.println("Ace"); break;
    case 11: System.out.println("Jack"); break;
    case 12: System.out.println("Queen"); break;
    case 13: System.out.println("King"); break;
    default: System.out.println(splayer2); break;
    } 
    // Determine card values
    dealervalue1 = dealer1; 
    switch(dealer1)
    {
    case 1:  dealervalue1=11; break;
    case 11:  case 12:  case 13:
        dealervalue1=10;  break;
    } // switch
    dealervalue2 = dealer2; 
    switch(dealer2)
    {
    case 1:  dealervalue2=11; break;
    case 11:  case 12:  case 13:
        dealervalue2=10;  break;
    } // switch
    dealerTotal = dealervalue1+dealervalue2;
    if (dealerTotal == 22)      dealerTotal = 12;
    playervalue1 = player1; 
    switch(player1)
    {
    case 1:  playervalue1=11; break;
    case 11:  case 12:  case 13:
        playervalue1=10;  break;
    } // switch
    playervalue2 = player2; 
    switch(player2)
    {
    case 1:  playervalue2=11; break;
    case 11:  case 12:  case 13:
        playervalue2=10;  break;
    } // switch
    playerTotal = playervalue1+playervalue2;
    if (playerTotal == 22)
        playerTotal = 12;
    if (playerTotal == 21)
    { 
        wager = wager * 1.5;
        JOptionPane.showMessageDialog(null, "You have a Blackjack!!! \n You won: $" + wager);

        return;
    }
    if (playerTotal >= dealerTotal)
    {
        System.out.println("" + wager);
        return;
    }
    JOptionPane.showMessageDialog(null, "You lost: $" + wager, "You lost!",  JOptionPane.ERROR_MESSAGE);

    System.out.println(swager);
} 

public static void main(String[] args) {
    // TODO Auto-generated method stub
    /**
     * 2
     * 2-card Blackjack game
     */
    Mdt1 black = new Mdt1();
}// main

}

I spend about 2 hours trying to understand why my code keeps throwing runtime errors. It doesn't want to accept user inputs... I'm pretty sure my implementaion of getext is correct. Could you guys take a look at my code and help me see what I did wrong?

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Mdt1 extends JFrame implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * @param args
 */
 JTextField twager, dcard1, dcard2, pcard1, pcard2;

public Mdt1()  {
    JFrame blackjack = new JFrame();
    Border wwager = BorderFactory.createTitledBorder("Wager");
    JPanel pwager = new JPanel();
    pwager.setBorder(wwager);
    JLabel lwager = new JLabel("Enter wager:");
    JTextField twager = new JTextField(6);
    twager.addActionListener(this);
    pwager.add(lwager);
    pwager.add(twager);

    JPanel pplayer = new JPanel();
    Border wayer = BorderFactory.createTitledBorder("Player");
    pplayer.setLayout(new BoxLayout(pplayer, BoxLayout.Y_AXIS));
    pplayer.setBorder(wayer);
    pplayer.setSize(100, 100);
    JTextField pcard1 = new JTextField(6);
    pcard1.setEditable(false);
    JTextField pcard2 = new JTextField(6);
    pcard2.setEditable(false);
    pplayer.add(pcard1);
    pplayer.add(pcard2);


    JPanel pdealer = new JPanel();
    Border wealer = BorderFactory.createTitledBorder("Dealer");
    pdealer.setLayout(new BoxLayout(pdealer, BoxLayout.Y_AXIS));
    pdealer.setBorder(wealer);
    pdealer.setSize(100, 100);
    JTextField dcard1 = new JTextField(6);
    dcard1.setEditable(false);
    JTextField dcard2 = new JTextField(6);
    dcard2.setEditable(false);
    pdealer.add(dcard1);
    pdealer.add(dcard2);

    JPanel cards = new JPanel();
    Border wards = BorderFactory.createTitledBorder("Cards");
    cards.setBorder(wards);
    cards.setSize(220, 80);
    cards.add(pplayer);
    cards.add(pdealer);

    JPanel pendgame = new JPanel();
    Border wesult = BorderFactory.createTitledBorder("Endgame");
    pendgame.setBorder(wesult);



    blackjack.setLayout(new BorderLayout());
    blackjack.setVisible(true);
    blackjack.setSize(200, 200);
    blackjack.setTitle("♣ Blackjack ♠");
    blackjack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    blackjack.add(pwager, BorderLayout.NORTH);
    blackjack.add(cards);
    blackjack.pack();
    blackjack.setResizable(false);

}

public void actionPerformed(ActionEvent e) {
    String swager = twager.getText();
    double wager;
    int  dealer1, dealer2, player1, player2;
    int  dealervalue1, dealervalue2, playervalue1, playervalue2;
    int  dealerTotal, playerTotal;


    wager = Double.parseDouble(swager);

    Random r=new Random();
    // card faces
    dealer1=r.nextInt(13) + 1;
    String sdealer1 = Integer.toString(dealer1);
    dealer2=r.nextInt(13) + 1;
    String sdealer2 = Integer.toString(dealer2);
    player1=r.nextInt(13) + 1;
    String splayer1 = Integer.toString(player1);
    player2=r.nextInt(13) + 1;
    String splayer2 = Integer.toString(player2);
    // Show dealer's card faces
    switch(dealer1)
    {
    case 1: dcard1.setText("Ace "); break;
    case 11: dcard1.setText("Jack"); break;
    case 12: dcard1.setText("Queen"); break;
    case 13: dcard1.setText("King"); break;
    default: dcard1.setText(sdealer1); break; 
    } 
    switch(dealer2)
    {
    case 1: dcard2.setText("Ace"); break;
    case 11: dcard2.setText("Jack"); break;
    case 12: dcard2.setText("Queen"); break;
    case 13: dcard2.setText("King"); break;
    default: dcard2.setText(sdealer2); break;
    } 
    // Show player's card faces
    switch(player1)
    {
    case 1: System.out.print("Ace"); break;
    case 11: System.out.print("Jack"); break;
    case 12: System.out.print("Queen"); break;
    case 13: System.out.print("King"); break;
    default: System.out.print(splayer1); break;
    } 
    switch(player2)
    {
    case 1: System.out.println("Ace"); break;
    case 11: System.out.println("Jack"); break;
    case 12: System.out.println("Queen"); break;
    case 13: System.out.println("King"); break;
    default: System.out.println(splayer2); break;
    } 
    // Determine card values
    dealervalue1 = dealer1; 
    switch(dealer1)
    {
    case 1:  dealervalue1=11; break;
    case 11:  case 12:  case 13:
        dealervalue1=10;  break;
    } // switch
    dealervalue2 = dealer2; 
    switch(dealer2)
    {
    case 1:  dealervalue2=11; break;
    case 11:  case 12:  case 13:
        dealervalue2=10;  break;
    } // switch
    dealerTotal = dealervalue1+dealervalue2;
    if (dealerTotal == 22)      dealerTotal = 12;
    playervalue1 = player1; 
    switch(player1)
    {
    case 1:  playervalue1=11; break;
    case 11:  case 12:  case 13:
        playervalue1=10;  break;
    } // switch
    playervalue2 = player2; 
    switch(player2)
    {
    case 1:  playervalue2=11; break;
    case 11:  case 12:  case 13:
        playervalue2=10;  break;
    } // switch
    playerTotal = playervalue1+playervalue2;
    if (playerTotal == 22)
        playerTotal = 12;
    if (playerTotal == 21)
    { 
        wager = wager * 1.5;
        JOptionPane.showMessageDialog(null, "You have a Blackjack!!! \n You won: $" + wager);

        return;
    }
    if (playerTotal >= dealerTotal)
    {
        System.out.println("" + wager);
        return;
    }
    JOptionPane.showMessageDialog(null, "You lost: $" + wager, "You lost!",  JOptionPane.ERROR_MESSAGE);

    System.out.println(swager);
} 

public static void main(String[] args) {
    // TODO Auto-generated method stub
    /**
     * 2
     * 2-card Blackjack game
     */
    Mdt1 black = new Mdt1();
}// main

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2024-12-16 16:13:28

您有实例字段 - JTextField twager, dcard1, dcard2, pcard1, pcard2; 但是在方法 Mdt1() 中,您将 JTextField 的引用存储到本地参考变量。

public Mdt1()  {
    JFrame blackjack = new JFrame();
    Border wwager = BorderFactory.createTitledBorder("Wager");
    JPanel pwager = new JPanel();
    pwager.setBorder(wwager);
    JLabel lwager = new JLabel("Enter wager:");
 //   JTextField twager = new JTextField(6); //<---  Local variable
    twager = new JTextField(6);              //<--- correction
    twager.addActionListener(this);
    pwager.add(lwager);
    pwager.add(twager);
    ....
}

You have instance fields - JTextField twager, dcard1, dcard2, pcard1, pcard2; however in method Mdt1() you stored references of JTextField into local reference variable.

public Mdt1()  {
    JFrame blackjack = new JFrame();
    Border wwager = BorderFactory.createTitledBorder("Wager");
    JPanel pwager = new JPanel();
    pwager.setBorder(wwager);
    JLabel lwager = new JLabel("Enter wager:");
 //   JTextField twager = new JTextField(6); //<---  Local variable
    twager = new JTextField(6);              //<--- correction
    twager.addActionListener(this);
    pwager.add(lwager);
    pwager.add(twager);
    ....
}
旧时模样 2024-12-16 16:13:28

您在构造函数中将类实例变量 twager 隐藏到了另一个同名变量中。实际上,您从未初始化过您的类实例。

You hid your class instance variable twager with another variable with the same name in your constructor. In effect, you never initialized your class instance.

傾城如夢未必闌珊 2024-12-16 16:13:28

在构造函数中,您将变量重新声明为类变量。

您的类变量是 JTextField twager、dcard1、dcard2、pcard1、pcard2;

在名为 Mdt1() 的构造函数中,您可以在其中初始化这些变量,并在本地创建它们。

例如,行JTextField twager = new JTextField(6);
如果您想解决问题,应将其更改为 twager= new JTextField(6);。

这应该可以解决问题。

In your constructor, you are redeclaring the variables you have as class variables.

Your class variables are JTextField twager, dcard1, dcard2, pcard1, pcard2;

In the constructor called Mdt1(), where you initialize these variables, you create them locally.

For example, the line JTextField twager = new JTextField(6);
should be changed to twager= new JTextField(6); if you want the problem fixed.

That should fix the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文