java报错空指针异常_java – 空指针异常错误,没有明显的代码错误
我在这里有一个错误,我不知道它来自哪里.我在初学者的
java课程是高中,所以我在这里还没有太多的经验.我有3个相互合并的程序.
我有一个卡片类,可以创建一张扑克牌
//********************************************************************
// Card.java Author: Lewis and Loftus
//
// Solution to Programming Project 4.5
//********************************************************************
import java.util.*;
public class Card
{
public final static int ACE = 1;
public final static int TWO = 2;
public final static int THREE = 3;
public final static int FOUR = 4;
public final static int FIVE = 5;
public final static int SIX = 6;
public final static int SEVEN = 7;
public final static int EIGHT = 8;
public final static int NINE = 9;
public final static int TEN = 10;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;
public final static int CLUBS = 1;
public final static int DIAMONDS = 2;
public final static int HEARTS = 3;
public final static int SPADES = 4;
private final static int NUM_FACES = 13;
private final static int NUM_SUITS = 4;
private int face,suit;
private String faceName,suitName;
private int myInt1,myInt2;
Random rand = new Random();
//-----------------------------------------------------------------
// Creates a random card.
//-----------------------------------------------------------------
public Card ()
{
face = rand.nextInt(4) + 1;
setFaceName();
suit = rand.nextInt(13) + 1;
setSuitName();
}
//-----------------------------------------------------------------
// Sets the string representation of the face using its stored
// numeric value.
//-----------------------------------------------------------------
private void setFaceName()
{
switch (face)
{
case 1:
faceName = "Ace";
break;
case 2:
faceName = "Two";
break;
case 3:
faceName = "Three";
break;
case 4:
faceName = "Four";
break;
case 5:
faceName = "Five";
break;
case 6:
faceName = "Six";
break;
case 7:
faceName = "Seven";
break;
case 8:
faceName = "Eight";
break;
case 9:
faceName = "Nine";
break;
case 10:
faceName = "Ten";
break;
case 11:
faceName = "Jack";
break;
case 12:
faceName = "Queen";
break;
case 13:
faceName = "King";
break;
}
}
//-----------------------------------------------------------------
// Sets the string representation of the suit using its stored
// numeric value.
//-----------------------------------------------------------------
private void setSuitName()
{
switch (suit)
{
case 1:
suitName = "Clubs";
break;
case 2:
suitName = "Diamonds";
break;
case 3:
suitName = "Hearts";
break;
case 4:
suitName = "Spades";
break;
}
}
//-----------------------------------------------------------------
// Determines if this card is higher than the passed card. The
// second parameter determines if aces should be considered high
// (beats a King) or low (lowest of all faces). Uses the suit
// if both cards have the same face.
//-----------------------------------------------------------------
public boolean isHigherThan (Card card2,boolean aceHigh)
{
boolean result = false;
if (face == card2.getFace())
{
if (suit > card2.getSuit())
result = true;
}
else
{
if (aceHigh && face == ACE)
result = true;
else
if (face > card2.getFace())
result = true;
}
return result;
}
//-----------------------------------------------------------------
// Determines if this card is higher than the passed card,// assuming that aces should be considered high.
//-----------------------------------------------------------------
public boolean isHigherThan (Card card2)
{
return isHigherThan (card2,true);
}
//-----------------------------------------------------------------
// Returns the face (numeric value) of this card.
//-----------------------------------------------------------------
public int getFace ()
{
return face;
}
//-----------------------------------------------------------------
// Returns the suit (numeric value) of this card.
//-----------------------------------------------------------------
public int getSuit ()
{
return suit;
}
//-----------------------------------------------------------------
// Returns the face (string value) of this card.
//-----------------------------------------------------------------
public String getFaceName ()
{
return faceName;
}
//-----------------------------------------------------------------
// Returns the suit (string value) of this card.
//-----------------------------------------------------------------
public String getSuitName ()
{
return suitName;
}
//-----------------------------------------------------------------
// Returns the string representation of this card,including
// both face and suit.
//-----------------------------------------------------------------
public String toString ()
{
return faceName + " of " + suitName;
}
}
我有一张Deck Of cards文件可以创建52张卡片
import java.util.*;
public class DeckOfCards
{
private Card deckOfCards[];
private int currentCardUsed;
private final int NumberOfCards = 52;
private int nextCard;
private Random rand;
String myString = "All Cards have been dealt.";
public DeckOfCards()
{
deckOfCards = new Card[NumberOfCards];
currentCardUsed = 0;
Random rand = new Random();
for(int index = 0; index < deckOfCards.length; index ++)
{
deckOfCards[index] = new Card();
}
}
public void shuffleCards()
{
currentCardUsed = 0;
for(int newCard = 0; newCard < deckOfCards.length; newCard ++)
{
int nextCard = rand.nextInt(NumberOfCards);
Card temporaryDeck = deckOfCards[newCard];
deckOfCards[newCard] = deckOfCards[nextCard];
deckOfCards[nextCard] = temporaryDeck;
}
}
public Card dealCard()
{
if(currentCardUsed < deckOfCards.length)
{
return deckOfCards[currentCardUsed ++];
}
else
{
return null;
}
}
}
最后我有一个司机班
public class DeckTester
{
public static void main(String [] args)
{
DeckOfCards deck = new DeckOfCards();
deck.shuffleCards();
System.out.println(deck.dealCard());
System.out.println(deck.dealCard());
System.out.println(deck.dealCard());
System.out.println(deck.dealCard());
System.out.println(deck.dealCard());
System.out.println(deck.dealCard());
}
}
他们都没有错误.但是当我运行驱动程序时,我得到了输出
Exception in thread "main" java.lang.NullPointerException
at DeckOfCards.shuffleCards(DeckOfCards.java:39)
at DeckTester.main(DeckTester.java:8)
我试过在交易方法中改变Null无济于事.
java报错空指针异常_java – 空指针异常错误,没有明显的代码错误相关推荐
- java报错 日志_java 日志报错
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-o ...
- java报错MalformedURLException: unknown protocol: c
java报错:MalformedURLException: unknown protocol: c 1. 报错情况: 部分代码: //打开图片path="C:/Users/MyUser/im ...
- java报错-找不到或无法加载主类(Error: Could not find or load main class)
此文首发于我的个人博客:java报错-找不到或无法加载主类(Error Could not find or load main class) - zhang0peter的个人博客 比如说test.ja ...
- linux安装python库报错pywin32_完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误...
报错信息 最近闲来无事,用python的tkinter库开发了一款带日程提醒的万年历桌面程序.在程序开发结束开始打包时,却发现一直报错 PyInstaller cannot check for ass ...
- java 报错 定位,问题定位分享(2)spark任务一定几率报错java.lang.NoSuchFieldError: HIVE_MOVE_FILES_THREAD_COUNT...
用yarn cluster方式提交spark任务时,有时会报错,报错几率是40%,报错如下: 18/03/15 21:50:36 116 ERROR ApplicationMaster91: User ...
- 安装 SQL Sever 2000至最后步骤报错“安装程序配置服务器失败。 参考服务器错误日志和 C: \Windows\sqltsp.log 了解更多信息”
在安装 SQL Sever 2000至最后步骤报错"安装程序配置服务器失败. 参考服务器错误日志和 C: \Windows\sqltsp.log 了解更多信息" ,如 图: 解决方 ...
- Mac安装pkg包报错:安装失败 “安装器遇到了一个错误,导致安装失败,请联系软件制造商以获得帮助
Mac安装pkg包报错:安装失败 "安装器遇到了一个错误,请联系软件制造商以获得帮助 打开终端,输入: sudo spctl --master-disable 然后输入锁屏密码. 然后在[系 ...
- win7 上面 gcc 编译的程序 a exe 运行的时候报错 a exe 已停止工作, 异常代码 c0000005
win7 上面 gcc 编译的程序 a.exe 运行的时候报错 a.exe 已停止工作, 异常代码:c0000005: 原因分析: 一开始我以为是我的代码的问题,后来查询了这个错误码发现原来是兼容性的 ...
- java报错空指针异常_springboot全局异常捕获,真香
全局异常捕获 什么是异常?程序在启动或者运行时没有按照预期的执行,在执行途中发生某种未知的错误,导致程序非正常停止或者报错. 在我们的程序中,肯定会伴随着很多的异常,启动时:空对象.找不到数据库.用户 ...
- java报错空指针异常_夯实基础:认识一下这10 个深恶痛绝的 Java 异常
异常是 Java 程序中经常遇到的问题,我想每一个 Java 程序员都讨厌异常,一 个异常就是一个 BUG,就要花很多时间来定位异常问题. 什么是异常及异常的分类请看这篇文章:异常小结:上一张图搞清楚 ...
最新文章
- python字符串find函数-python常见字符串处理函数与用法汇总
- 将单链表的每K个节点之间逆序
- git push/pull时总需要输入用户名密码的解决方案
- 干货:结合Scikit-learn介绍几种常用的特征选择方法
- image 微信小程序flex_第三天学习微信小程序开发总结
- pjlib深入剖析和使用详解
- python kivy kv模板调用_正确使用.kv文件进行Kivy并将其导入到Python...
- 安卓饼状图设置软件_Android自定义控件实现饼状图
- python时间格式转换为美式日期_python中有关时间日期格式转换问题
- 阿里巴巴矢量图标库使用在线字体图标
- jpg转pdf怎么转换免费
- 吸引子传播(Affinity Propagation)算法
- grep -A -B -C
- Dango Web 开发指南 学习笔记 1
- 从PD充电器取9V/12V给产品供电快充,PD取电芯片概述
- 计算机辅助翻译实验室,计算机辅助翻译实验室建设及应用探索.pdf
- 关于android(安卓)模拟器加速
- 模棱两可的生物学概念问题辨析1
- 九成宫醴泉铭-欧阳询(慢更)
- Esri携“新一代Web GIS”亮相中国地理信息产业大会
热门文章
- HDC.2019后再发力,AppGallery Connect服务新升级
- IEEE 回应禁止华为系审稿人;WiFi联盟、蓝牙联盟已恢复华为成员资格;中国计算机学会:暂时中止与IEEE通信学会合作……...
- 全球再迎超级飓风,黑客可利用微软“蠕虫级”高危漏洞暴击全球
- 电脑桌面归纳小窗口_电脑一分钟小技巧:如何将电脑设置为定时关机?
- python多线程同步与互斥_python多线程编程(3): 使用互斥锁同步线程
- 获取本地ip地址适用于windows和Linux环境
- Guns 下载、导入、运行_入门试炼01
- 第九篇:Spring Boot整合Spring Data JPA_入门试炼04
- EditPlus连接远程Linux虚拟机
- apache gobblin mysql_incubator-gobblin-master