public class tetris
{
        static class Piece
                {
                        Point p1,p2,p3,pivot;
                        Color c;
                }
             
                public static void CreePiece(Color[][] t,Grille g)
                {
                        
                        int i;
                        switch (i = (int)(Math.Random()*100)%7){
                        case 0 ://Piece I
                                Piece p = new Piece();
                              
                             //   Piece p = new Piece();
                                p.p1.x = 0;
                                p.p2.x = 4;
                                p.p3.x = 4;
                                p.pivot.x = 4;
                                p.p1.y = 0;
                                p.p2.y = 1;
                                p.p3.y = 2;
                                p.pivot.y = 3;
                                p.c = Color.RED;
                        
                        break;
                                case 1 ://Piece O
                      
                               
                                p.p1.x = 4;
                                p.p2.x = 5;
                                p.p3.x = 4;
                                p.pivot.x = 5;
                                p.p1.y = 0;
                                p.p2.y = 0;;
                                p.p3.y = 1;
                                p.pivot.y = 1;
                                p.c = Color.BLUE;
                        
                        break;
                case 2: //Piece T
                        
                                //Piece p = new Piece();
                                p.p1.x = 3;
                                p.p2.x = 4;
                                p.p3.x = 5;
                                p.pivot.x = 4;
                                p.p1.y = 0;
                                p.p2.y = 0;
                                p.p3.y = 0;
                                p.pivot.y = 1;
                                p.c = Color.YELLOW;
                        
                        break;
                case 3://Piece J
                        
                                //Piece p = new Piece();
                                p.p1.x = 4;
                                p.p2.x = 4;
                                p.p3.x = 4;
                                p.pivot.x = 3;
                                p.p1.y = 0;
                                p.p2.y = 1;
                                p.p3.y = 2;
                                p.pivot.y = 2;
                                p.c = Color.GREEN;
                        
                        break;
                case 4://Piece L
                        
                                //Piece p = new Piece();
                                p.p1.x = 4;
                                p.p2.x = 4;
                                p.p3.x = 4;
                                p.pivot.x = 5;
                                p.p1.y = 0;
                                p.p2.y = 1;
                                p.p3.y = 2;
                                p.pivot.y = 2;
                                p.c = Color.WHITE;
                        
                        break;
                case 5://Piece Z 
                        
                                //Piece p = new Piece();
                                p.p1.x = 3;
                                p.p2.x = 4;
                                p.p3.x = 4;
                                p.pivot.x = 5;
                                p.p1.y = 0;
                                p.p2.y = 0;
                                p.p3.y = 1;
                                p.pivot.y = 1;
                                p.c = Color.RED;
                        
                        break;
                case 6://Piece S
                        
                                //Piece p = new Piece();
                                p.p1.x = 4;
                                p.p2.x = 5;
                                p.p3.x = 4;
                                p.pivot.x = 3;
                                p.p1.y = 0;
                                p.p2.y = 0;
                                p.p3.y = 1;
                                p.pivot.y = 1;
                                p.c = Color.BLUE;
                        
                        break;
                       */ 
                        }
                       
                        g.modifCase(p.p1.x,p.p2.y,p.c);
                        g.modifCase(p.p2.x,p.p2.y,p.c);
                        g.modifCase(p.p3.x,p.p3.y,p.c);
                        g.modifCase(p.pivot.x,p.pivot.y,p.c);
                }
 public static void main (String[] args) 
{
    Grille g = new Grille("Tetris",10,10,Color.white);    CreePiece(t,g);
}报错 p.p1.x 空指针,CreePiece(t.g);空指针。这个是随机出来7种图形的一种,但是出现不了(可以编译)
求大侠!!

解决方案 »

  1.   

    import java.awt.Color;
    import java.awt.Point;
    public class Tetris {
    static class Piece {
    Point p1, p2, p3, pivot;
    Color c;
    } public static void CreePiece(Grille g) {
    int i;
    Piece p = new Piece();//报空指针,主要是作用域问题。
    switch (i = (int)(Math.random() * 100) % 7) {
    case 0:// Piece I
    // Piece p = new Piece();
    p.p1.x = 0;
    p.p2.x = 4;
    p.p3.x = 4;
    p.pivot.x = 4;
    p.p1.y = 0;
    p.p2.y = 1;
    p.p3.y = 2;
    p.pivot.y = 3;
    p.c = Color.RED; break;
    case 1:// Piece O p.p1.x = 4;
    p.p2.x = 5;
    p.p3.x = 4;
    p.pivot.x = 5;
    p.p1.y = 0;
    p.p2.y = 0;
    ;
    p.p3.y = 1;
    p.pivot.y = 1;
    p.c = Color.BLUE; break;
    case 2: // Piece T // Piece p = new Piece();
    p.p1.x = 3;
    p.p2.x = 4;
    p.p3.x = 5;
    p.pivot.x = 4;
    p.p1.y = 0;
    p.p2.y = 0;
    p.p3.y = 0;
    p.pivot.y = 1;
    p.c = Color.YELLOW; break;
    case 3:// Piece J // Piece p = new Piece();
    p.p1.x = 4;
    p.p2.x = 4;
    p.p3.x = 4;
    p.pivot.x = 3;
    p.p1.y = 0;
    p.p2.y = 1;
    p.p3.y = 2;
    p.pivot.y = 2;
    p.c = Color.GREEN; break;
    case 4:// Piece L // Piece p = new Piece();
    p.p1.x = 4;
    p.p2.x = 4;
    p.p3.x = 4;
    p.pivot.x = 5;
    p.p1.y = 0;
    p.p2.y = 1;
    p.p3.y = 2;
    p.pivot.y = 2;
    p.c = Color.WHITE; break;
    case 5:// Piece Z // Piece p = new Piece();
    p.p1.x = 3;
    p.p2.x = 4;
    p.p3.x = 4;
    p.pivot.x = 5;
    p.p1.y = 0;
    p.p2.y = 0;
    p.p3.y = 1;
    p.pivot.y = 1;
    p.c = Color.RED; break;
    case 6:// Piece S // Piece p = new Piece();
    p.p1.x = 4;
    p.p2.x = 5;
    p.p3.x = 4;
    p.pivot.x = 3;
    p.p1.y = 0;
    p.p2.y = 0;
    p.p3.y = 1;
    p.pivot.y = 1;
    p.c = Color.BLUE; break;
    } g.modifCase(p.p1.x, p.p2.y, p.c);
    g.modifCase(p.p2.x, p.p2.y, p.c);
    g.modifCase(p.p3.x, p.p3.y, p.c);
    g.modifCase(p.pivot.x, p.pivot.y, p.c);
    } public static void main(String[] args){
    Grille g = new Grille("Tetris", 10, 10, Color.white);

    CreePiece(g);//报空指针是正常的,都没看见你创建t。还有发现你的代码根本没用到t,先帮你删了
    }
    }class Grille{//不知道你这个类是干嘛用的。 public Grille(String string, int i, int j, Color white){

    } public void modifCase(int x, int y, Color c){

    }

    }
      

  2.   

    同意1楼的,你在case0中初始化了对象,却要在case1中访问,这是会报空指针的
      

  3.   

    那个t忘了加了,是有
    Color[][] t = new Color[10][10];
    CreePiece(t,g);class Grille 是老师给的 interface,可以直接使用.现在问题在于p.p1.x 和 p.p1.y 这种老是报空指针,导致不能显示。应该是Piece p = new Piece(); 放置的问题???
      

  4.   

    Color[][] t = new Color[10][10];
    这也不行啊.
    t只是个空盒子,里面没有Color对象。
    for(int i=0;i<10;i++){
    for(int j=0;j<10;j++)
    t[i][j] = new Color(...);
    }现在问题在于p.p1.x 和 p.p1.y 这种老是报空指针,导致不能显示。
    应该是Piece p = new Piece(); 放置的问题???
       恩,放置问题。
      

  5.   

    那Piece p = new Piece(); 应该放在那里呢??
      

  6.   


        public static void CreePiece(Grille g) {
        
            Piece p = new Piece();//报空指针,主要是作用域问题。
            p.p1 = new Point();
            p.p2 = new Point();
            p.p3 = new Point();
            p.pivot = new Point();
            switch ((int)(Math.random() * 100) % 7) {我错了。呵呵,大意了
      

  7.   

    恩,这下好了,谢谢哈。还有个问题,旋转那个函数怎么写啊?(坐标轴的那个,一某一个点为中心点旋转,用sin,cos??)