2014-03-27 194 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'状态是我想知道的。 –

相关问题