似乎无法弄清楚为什么我一直在Java套接字网络中获得NullPointerExceptions

时间:2018-04-02 11:04:07

标签: java sockets networking ip logic

所以我正在编写一个Battleship程序,我已经接近完成了我的客户端和服务器。我的所有其他类都在工作(这篇文章中的方法引用和我在下面发布的代码,但它们似乎工作正常,所以我认为这是逻辑),除了我的核心游戏逻辑之外,似乎。放置船只后,我的客户端和服务器都决定关闭。取决于运行,有时它确实到达最后一个方法(发生打印),startPVP3()(在下面的代码中),但它仍然会导致此错误,仍然崩溃并有奇怪的打印消息,如打印“成功连接到服务器“迟到,你可以在输出中看到。

出于某种原因,这些是我得到的输出。我把220之类的数字标签用于查明问题,但它似乎没什么帮助。我也有稍微不同的系统放置船只,因为与服务器相同的系统给客户带来了更多的问题。

简而言之,这里的实际问题是什么?导致这些错误的原因以及为什么两个类(不是同一个项目)的代码中的startPVP2()方法中的循环行为如此奇怪?我是网络新手,我已经看了几个小时,但仍然无法理解。

我合法地无法弄清楚这段代码有什么问题。

非常感谢帮助!

服务器主类代码:https://pastebin.com/4BS9qwHV

客户主要类代码:https://pastebin.com/v4vE108Z

(代码在pastebin中因为太长了)

服务器输出:

  run:
    ===================================
    Welcome to the game of Battleship!
    ===================================
    Type "rules" if you don't know the rules. 
    Type "pvp" if you want to play against another player via network connectivity! 
    Type "quit" if you want to quit the game. 

    pvp

    Connect your client.

    Socket Opened
    Place your ships...

      1 2 3 4 5 6 7 8 9 10
    A - - - - - - - - - - 
    B - - - - - - - - - - 
    C - - - - - - - - - - 
    D - - - - - - - - - - 
    E - - - - - - - - - - 
    F - - - - - - - - - - 
    G - - - - - - - - - - 
    H - - - - - - - - - - 
    I - - - - - - - - - - 
    J - - - - - - - - - - 

    Where would you like to place a ship?
    Remaining to place: 5
    a1

    What type of ship? Sizes - 1, 2, 3, 4, 5
    1

    Direction?
    1
      1 2 3 4 5 6 7 8 9 10
    A X - - - - - - - - - 
    B - - - - - - - - - - 
    C - - - - - - - - - - 
    D - - - - - - - - - - 
    E - - - - - - - - - - 
    F - - - - - - - - - - 
    G - - - - - - - - - - 
    H - - - - - - - - - - 
    I - - - - - - - - - - 
    J - - - - - - - - - - 

    Where would you like to place a ship?
    Remaining to place: 4
    b1

    What type of ship? Sizes - 1, 2, 3, 4, 5
    1

    Direction?
    1
      1 2 3 4 5 6 7 8 9 10
    A X - - - - - - - - - 
    B X - - - - - - - - - 
    C - - - - - - - - - - 
    D - - - - - - - - - - 
    E - - - - - - - - - - 
    F - - - - - - - - - - 
    G - - - - - - - - - - 
    H - - - - - - - - - - 
    I - - - - - - - - - - 
    J - - - - - - - - - - 

    Where would you like to place a ship?
    Remaining to place: 3
    c1

    What type of ship? Sizes - 1, 2, 3, 4, 5
    1

    Direction?
    1
      1 2 3 4 5 6 7 8 9 10
    A X - - - - - - - - - 
    B X - - - - - - - - - 
    C X - - - - - - - - - 
    D - - - - - - - - - - 
    E - - - - - - - - - - 
    F - - - - - - - - - - 
    G - - - - - - - - - - 
    H - - - - - - - - - - 
    I - - - - - - - - - - 
    J - - - - - - - - - - 

    Where would you like to place a ship?
    Remaining to place: 2
    d1

    What type of ship? Sizes - 1, 2, 3, 4, 5
    1

    Direction?
    1
      1 2 3 4 5 6 7 8 9 10
    A X - - - - - - - - - 
    B X - - - - - - - - - 
    C X - - - - - - - - - 
    D X - - - - - - - - - 
    E - - - - - - - - - - 
    F - - - - - - - - - - 
    G - - - - - - - - - - 
    H - - - - - - - - - - 
    I - - - - - - - - - - 
    J - - - - - - - - - - 

    Where would you like to place a ship?
    Remaining to place: 1
    e2

    What type of ship? Sizes - 1, 2, 3, 4, 5
    1

    Direction?
    1
    Ready to move on?
    ready
    ready198
    Exception in thread "main" java.lang.NullPointerException
        at battleship.Battleship.startPVP2(Battleship.java:200)
        at battleship.Battleship.startPVP(Battleship.java:131)
        at battleship.Battleship.intro(Battleship.java:47)
        at battleship.Battleship.main(Battleship.java:20)
    ~/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
    BUILD FAILED (total time: 39 seconds)

客户端输出:

run:
===================================
Welcome to the game of Battleship!
===================================
Type "rules" if you don't know the rules. 
Type "pvp" if you want to play against another player via network connectivity! 
Type "quit" if you want to quit the game. 

pvp

Establishing Connection
Connecting
...Successful connection to server!
Place your ships...

  1 2 3 4 5 6 7 8 9 10
A - - - - - - - - - - 
B - - - - - - - - - - 
C - - - - - - - - - - 
D - - - - - - - - - - 
E - - - - - - - - - - 
F - - - - - - - - - - 
G - - - - - - - - - - 
H - - - - - - - - - - 
I - - - - - - - - - - 
J - - - - - - - - - - 

Where would you like to place the ship of size2?

a1

Direction?
1
  1 2 3 4 5 6 7 8 9 10
A X - - - - - - - - - 
B X - - - - - - - - - 
C - - - - - - - - - - 
D - - - - - - - - - - 
E - - - - - - - - - - 
F - - - - - - - - - - 
G - - - - - - - - - - 
H - - - - - - - - - - 
I - - - - - - - - - - 
J - - - - - - - - - - 

Where would you like to place the ship of size3?

b3

Direction?
1
  1 2 3 4 5 6 7 8 9 10
A X - - - - - - - - - 
B X - X - - - - - - - 
C - - X - - - - - - - 
D - - X - - - - - - - 
E - - - - - - - - - - 
F - - - - - - - - - - 
G - - - - - - - - - - 
H - - - - - - - - - - 
I - - - - - - - - - - 
J - - - - - - - - - - 

Where would you like to place the ship of size4?

c4

Direction?
1
  1 2 3 4 5 6 7 8 9 10
A X - - - - - - - - - 
B X - X - - - - - - - 
C - - X X - - - - - - 
D - - X X - - - - - - 
E - - - X - - - - - - 
F - - - X - - - - - - 
G - - - - - - - - - - 
H - - - - - - - - - - 
I - - - - - - - - - - 
J - - - - - - - - - - 

Where would you like to place the ship of size5?

a7

Direction?
1
220Successful connection to server!
Exception in thread "main" java.lang.NullPointerException
    at battleshipmyclient.Battleship.startPVP2(Battleship.java:219)
    at battleshipmyclient.Battleship.startPVP(Battleship.java:143)
    at battleshipmyclient.Battleship.intro(Battleship.java:46)
    at battleshipmyclient.Battleship.main(Battleship.java:19)
~/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 39 seconds)

0 个答案:

没有答案