2014-03-27 193 views
0

假設我有3個類,A類,B類和C類。我將類A & B用作靜態類,將C類用作對象。Java-將實例從一個類傳遞到另一個類

C類看起來是這樣的:

public class C { 

    int someInformation; 

    public void setInfo(int newInfo){ 
     someInformation=newInfo; 
    } 
} 

在B組中,我創建這個對象的實例,並嘗試將其傳遞給函數在A類

public void someFunction(){ 
     ClassC classC = new ClassC(); 
     ClassA.doSomething(classC, 6); 
    } 

如果在DoSomething的,我嘗試使用

classC.setInfo(integerInputed); 
在ClassA的

,我得到一個NullPointerException。我究竟做錯了什麼?

在這些課程中,我試圖模擬建造戰艦板。

真正的C類如下:

package boardBuilder; 
import java.util.Random; 

public class Ship { 

int[] x; 
int[] y; 
int shipSize; 

public void setSize(int setSizeSize){ 
    shipSize = setSizeSize; 
    x = new int[shipSize]; 
    y = new int[shipSize]; 
} 

public int getSize(){ 
    return shipSize; 
} 

public int[] getX(){ 
    return x; 
} 

public int[] getY(){ 
    return y; 
} 

public void createShipForm(){ 
    Random rnd = new Random(); 
    x[0]=0; 
    y[0]=0; 
    int direction; 
    for(int extraPlace = 1; extraPlace+1<=shipSize; extraPlace++){ 
     direction = Math.abs(rnd.nextInt()%4); 
     if(direction==0){ 
      x[extraPlace]=x[extraPlace-1]; 
      y[extraPlace]=y[extraPlace-1]+1; 
     } 
     if(direction==1){ 
      x[extraPlace]=x[extraPlace-1]+1; 
      y[extraPlace]=y[extraPlace-1]; 
     } 
     if(direction==2){ 
      x[extraPlace]=x[extraPlace-1]; 
      y[extraPlace]=y[extraPlace-1]-1; 
     } 
     if(direction==3){ 
      x[extraPlace]=x[extraPlace-1]-1; 
      y[extraPlace]=y[extraPlace-1]; 
     } 
     for(int checkLocation=0;checkLocation<extraPlace;checkLocation++){ 
      if(x[extraPlace]==x[checkLocation]&&y[extraPlace]==y[checkLocation]){ 
       extraPlace--; 
       break; 
      } 
     } 
    } 
}   
} 

真正的A類如下:

package boardBuilder; 

public class BoardBuilder { 
    int[][] board; 
    Ship[][] ships; 

public void initializeBoard(int boardWidth, int boardLength, int num1, int num2, int num3, int num4, int num5, int num6){ 
    board = new int[boardWidth][boardLength]; 
    Ship[] ships1 = new Ship[num1]; 
    Ship[] ships2 = new Ship[num2]; 
    Ship[] ships3 = new Ship[num3]; 
    Ship[] ships4 = new Ship[num4]; 
    Ship[] ships5 = new Ship[num5]; 
    Ship[] ships6 = new Ship[num6]; 
    Ship[][] shipsCopy = {ships1, ships2, ships3, ships4, ships5, ships6}; 
    ships = shipsCopy; 
} 

public void setShips(){ 
    int count6 = ships[5].length; 
    int count5 = ships[4].length; 
    int count4 = ships[3].length; 
    int count3 = ships[2].length; 
    int count2 = ships[1].length; 
    int count1 = ships[0].length; 
    for(int set6 = 1; set6 <= count6; set6++){ 
     board = AddShip.addShip(board, 6, ships[5][set6-1]); 
    } 
    for(int set5 = 1; set5 <= count5; set5++){ 
     board = AddShip.addShip(board, 5, ships[4][set5-1]); 
    } 
    for(int set4 = 1; set4 <= count4; set4++){ 
     board = AddShip.addShip(board, 4, ships[3][set4-1]); 
    } 
    for(int set3 = 1; set3 <= count3; set3++){ 
     board = AddShip.addShip(board, 3, ships[2][set3-1]); 
    } 
    for(int set2 = 1; set2 <= count2; set2++){ 
     board = AddShip.addShip(board, 2, ships[1][set2-1]); 
    } 
    for(int set1 = 1; set1 <= count1; set1++){ 
     board = AddShip.addShip(board, 1, ships[0][set1-1]); 
    } 
} 

public int[][] getBoard(){ 
    return board; 
} 

public Ship[][] getShips(){ 
    return ships; 
} 
} 

在setShips的線8()方法,我輸入船舶對象。

真實的B類:我得到這個類的第19行的錯誤。

package boardBuilder; 

import java.util.Random; 

public class AddShip { 
    public static int[][] addShip(int[][] board, int size, Ship ship1){ 
     Ship ship = new Ship(); 
     ship = ship1; 
     boolean shipPlaced = false; 
     int[][] boardCopy = board; 
     int[] shipX = new int[size]; 
     int[] shipY = new int[size]; 
     int yPosition = 0; 
     int xPosition = 0; 
     int width = board.length; 
     int height = board[0].length; 
     while(!shipPlaced){ 
     Random rnd = new Random(); 
     ship.setSize(size); 
     ship.createShipForm(); 
     shipX = ship.getX(); 
     shipY = ship.getY(); 
     for(int placeAttempt = 1; placeAttempt <=10&!shipPlaced; placeAttempt++){ 
      shipPlaced=true; 
      xPosition = Math.abs(rnd.nextInt()%width); 
      yPosition = Math.abs(rnd.nextInt()%height); 
      for(int setCoordinate = 0; setCoordinate<size;setCoordinate++){ 
       if(xPosition + shipX[setCoordinate]>=0&xPosition + shipX[setCoordinate]<width&yPosition + shipY[setCoordinate]>=0&yPosition + shipY[setCoordinate]<height){ 
       if(boardCopy[xPosition + shipX[setCoordinate]][yPosition + shipY[setCoordinate]] == 0){ 
        boardCopy[xPosition + shipX[setCoordinate]][yPosition + shipY[setCoordinate]] = 2; 
       }else{ 
        shipPlaced=false; 
        break; 
       } 
       }else{ 
        shipPlaced=false; 
        break; 
       } 

      } 
      if(shipPlaced){ 
       boardCopy=surroundShip(boardCopy, shipX, shipY, xPosition, yPosition); 
       break; 
      } 
      if(shipPlaced==false){ 
       boardCopy=board; 
      } 
     } 
     } 
     board = boardCopy; 
     return board; 
    } 

    public static int[][] surroundShip(int[][] board, int[] shipX, int[] shipY, int startX, int startY){ 
     int shipSize = shipX.length; 
     int width = board.length; 
     int length = board[0].length; 
     for(int coordinate = 0; coordinate<shipSize;coordinate++){ 
      for(int relX = -1; relX<=1; relX++){ 
       for(int relY = -1; relY<=1; relY++){ 
        if(relX+shipX[coordinate]+startX<width&relX+shipX[coordinate]+startX>=0&relY+shipY[coordinate]+startY<length&relY+shipY[coordinate]+startY>=0){ 
        if(board[relX+shipX[coordinate]+startX][relY+shipY[coordinate]+startY]!=2){ 
         board[relX+shipX[coordinate]+startX][relY+shipY[coordinate]+startY]=5; 
        } 
        } 
       } 
      } 
     } 
     return board; 
    } 
} 
+9

發佈您的實際代碼和一個真正的堆棧跟蹤。 –

+3

是的,告訴我們'ClassA'。否則,這是一個水晶球凝視練習。 –

回答

0
ClassC classc = new ClassC(); ClassA.doSomething(classC, 6); 

對象名稱是classc或classC?

+0

根據「靜態類」的意思,你[甚至沒有錯](http://en.wikipedia.org/wiki/Not_even_wrong)。 –

+1

@Elliot類名:ClassC,給定對象名:classC,doSomething()中的參數 - classC – attaboy182

+0

從ClassB靜態方法調用'ClassA.doSomething'。爲什麼你會在沒有工廠或構建器的情況下從'ClassA'中的靜態方法初始化'ClassC'狀態是我想知道的。 –

相關問題