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無濟于事.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/530011.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/530011.shtml
英文地址,請注明出處:http://en.pswp.cn/news/530011.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql表的設計幾種方式_支持多種登錄方式的數據表設計 | 六阿哥博客

一個帶有用戶系統的應用最基本登錄方式是站內賬號登錄&#xff0c;但這種方式往往不能滿足我們的需求。現在的應用基本都有站內賬號、email、手機和一堆第三方登錄&#xff0c;那么如果需要支持這么多種登錄方式&#xff0c;或者還有銀行卡登錄、身份證登錄等等更多的登錄方式&…

將Go語言開發的Web程序部署到K8S

搭建K8S基礎環境 如果已經有K8S環境的同學可以跳過&#xff0c;如果沒有&#xff0c;推薦你看看我的《Ubuntu22加Minikue搭建K8S環境》&#xff0c;課程目錄如下&#xff1a; Ubuntu22安裝Vscode 下載&#xff1a;https://code.visualstudio.com/Download 安裝命令&#…

python 掃描儀_基于Opencv和Python的多選掃描儀

首先&#xff0c;我檢測到圖像右側的20個黑框&#xff0c;然后將x和寬度添加到列表中&#xff1a;image cv2.imread(args["image"])gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)(_, thresh) cv2.threshold(gray, 220, 255,cv2.THRESH_BINARY)kernel cv2.getStr…

mysql dmz_MySQL 中LIMIT的使用詳解

MySQL的Limit子句Limit子句可以被用于強制 SELECT 語句返回指定的記錄數。Limit接受一個或兩個數字參數。參數必須是一個整數常量。如果給定兩個參數&#xff0c;第一個參數指定第一個返回記錄行的偏移量&#xff0c;第二個參數指定返回記錄行的最大數目。//初始記錄行的偏移量…

python編程入門到實踐筆記習題_Python編程從入門到實踐筆記——列表簡介

python編程從入門到實踐筆記——列表簡介#codingutf-8#列表——我的理解等于c語言和java中的數組bicycles ["trek","cannondale","readline","specialized"]print(bicycles)#列表索引從0開始print(bicycles[0].title())#訪問列表元素…

informatica mysql odbc_Informatica 配置mysql community odbc連接

Informatica linux 版本內置的DataDirect 驅動支持各種數據庫例如oracle、sybase、postgreSQL、Greenplum、mysql等等但是mysql 只支持企業版本&#xff0c;如果我們使用的是community 社區版本便不能使用自帶的DataDirect方式了&#xff0c;那我們就需要手動配置其他odbc連接。…

mysql分表 動態擴容_數據庫hash分表后的擴容方案

postgres的hash分表不停機擴容方案原來我們hash分表之后&#xff0c;數據擴容采用的是rehash&#xff0c;這樣遷移全部的數據&#xff0c;比較麻煩。本次擴容利用hash環原理&#xff0c;并在此基礎上做一些適應性的改動。首先假定哈希環的范圍為0-1023&#xff0c;總共1024的數…

php mysql長連接聊天室_PHP之探索MySQL 長連接、連接池

PHP連接MysqL的方式&#xff0c;用的多的是MysqL擴展、MysqLi擴展、pdo_MysqL擴展,是官方提供的。PHP的運行機制是頁面執行完會釋放所有該PHP進程中的所有資源的&#xff0c;如果有多個并發訪問本地的測試頁面 http://127.0.0.1/1.php 根據PHP跟web服務器的不同&#xff0c;會開…

python 讀取地震道頭數據_python地震數據可視化詳解

本文實例為大家分享了python地震數據可視化的具體代碼&#xff0c;供大家參考&#xff0c;具體內容如下準備工作&#xff1a;在windows10下安裝python3.7&#xff0c;下載參考源碼到本地。1. demo繪圖測試demo繪圖指令cmd> python seisplot.py --demo問題1)缺少依賴包File &…

在MySQL查詢山東省男生信息_MySQL-查詢

來一波英語單詞解釋(意思)create 創建show 顯示database 數據庫use 使用select 選擇table 表from 來自…distinct 消除重復行as 同樣地(用于其別名)where 范圍like 模糊查詢rlike 正則查詢In 范圍查詢not in 不非連續的范圍之內between ... and …表示…

java 導入world數據_java讀取world文件,把world文件中的內容,原樣輸出到頁面上。...

POI,處理可以。樣式在Java代碼中添加就可以。給了一個例子這個是Excel的。package cn.com.my.common;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.sql.Connection;import java.sql.ResultSet…

java程序員 css_Java程序員從笨鳥到菜鳥之(十七)CSS基礎積累總結(下)

七.組織元素(span和div)span和div元素用于組織和結構化文檔&#xff0c;并經常聯合class和id屬性一起使用。在這一課中&#xff0c;我們將進一步探究span和div的用法&#xff0c;因為這兩個HTML元素對于CSS是很重要的。用span組織元素用div組織元素用span組織元素span元素可以說…

redlock java_Redlock分布式鎖

這篇文章主要是對 Redis 官方網站刊登的 Distributed locks with Redis 部分內容的總結和翻譯。什么是 RedLockRedis 官方站這篇文章提出了一種權威的基于 Redis 實現分布式鎖的方式名叫 Redlock&#xff0c;此種方式比原先的單節點的方法更安全。它可以保證以下特性&#xff1…

java 兩個數組交叉_java – 如何交叉兩個沒有重復的排序整數數組?

這個問題本質上減少到一個連接操作,然后是一個過濾器操作(刪除重復,只保留內部匹配).由于輸入都已經排序,所以可以通過O(O(size(a)size(b))的merge join來有效地實現連接.過濾器操作將為O(n),因為連接的輸出被排序,并且要刪除重復項,所有您需要做的是檢查每個元素是否與之??前…

java retentionpolicy_Java注解之如何利用RetentionPolicy.SOURCE生存周期

上一篇文章簡單講了下Java注解的學習之元注解說明&#xff0c;學習了Java注解是如何定義的&#xff0c;怎么使用的&#xff0c;但是并沒有介紹Java的注解是怎么起作用的&#xff0c;像Spring Boot里面的那些注解&#xff0c;到底是怎么讓程序這樣子運行起來的&#xff1f;特別是…

在java程序中定義的類有兩種成員_java試題 急需答案 謝謝!!!

三、填空(每小題2分&#xff0c;共10分)1&#xff0e;在Applet中&#xff0c;創建一個具有10行45列的多行文本區對象ta的語句為&#xff1a;2&#xff0e;創建一個標識有“關閉”字樣的標簽對象gb的語句為。3&#xff0e;方法是一種僅有方法頭&#xff0c;沒...三、填空(每小題…

java 同步 變量,在java中的對象上同步,然后更改同步的變量的值

I came across a code like thissynchronized(obj) {obj new Object();}Something does not feel right about this , I am unable to explain, Is this piece of code OK or there is something really wrong in it, please point it out.Thanks解決方案Its probably not wha…

java set泛型_Java 集合二 泛型、Set相關

泛型1、在定義一個類的方法時&#xff0c;因為不確定返回值類型&#xff0c;所以用一個符號代替&#xff0c;這個符號就是泛型eg:ArrayList list new ArrayList();2、泛型的好處&#xff1a;1、提高了數據的安全性&#xff0c;將運行時的問題提前暴露在編譯階段2、避免了強轉的…

java annotation 實現_在Java中如何實現自己的annotation

1. 先定義annotation2. 使用annotation例子&#xff1a;import java.lang.annotation.*;import java.lang.reflect.Method;Target(ElementType.METHOD)Retention(RetentionPolicy.RUNTIME)interface Test {String info() default "";}class Annotated {Test(info &q…

登錄界面攔截java_java攔截通過url訪問頁面,必須通過登錄頁面訪問目標頁面

在web.xml中配置過濾&#xff1a;LoginFiltercom.verification.action.LoginFilterLoginFiltery/form/dealParse.do/* 攔截所有請求/.do 攔截以“.do”結尾的請求/index.jsp 攔截指定的jsp/artery/form/* 攔截該目錄下的所有請求等等攔截器&#xff0c;攔截請求類&#xf…