This site requires JavaScript, please enable it in your browser!
Greenfoot back
S02
S02 wrote ...

2014/9/12

Null Pointer while creating World

1
2
S02 S02

2014/9/12

#
My ABC class is calling a function setMessage(String msg) which is defined in base class. It is giving null pointer exception. The setMassage(String msg function is as below: protected void setMessage( String msg ) { int X, Y ; m.setText( msg ); X=getX(); Y=getY(); World world = getWorld(); if ( m.getWorld() != null ) { world.removeObject( m ) ; return; } world.addObject(m, X,Y ) ; } I can see the value of world object is coming as null. Please help me out in this. Am i missing something. Please let me know if more information required. Thanks In Advance.
danpost danpost

2014/9/12

#
Is the base object in the world when you call the 'setMessage(String)' method? It is really difficult to understand what you are doing. 'ABC' and 'base' and 'm' are vague descriptors for classes and fields. What behavior is each supposed to have? Also, what exactly is this method supposed to do? It looks like it could either add or remove the 'm' actor to or from the world.
Super_Hippo Super_Hippo

2014/9/12

#
I guess you use the method from a constructor. You can't use methods like 'getX()' there, because the object is not placed in the world yet when the constructor code is executed. You can use the 'addedToWorld' method which is executed automatically right after the object is placed in the world. I also wonder what you want to do there actually. You set a Text to an object (reference 'm') and if this object exists, you delete it. By the way, you don't need to get a reference to your world when using regular world methods, you can just use 'getWorld().addObject(...)'. (I am wondering when the Greenfoot team will increase the size of the "Posting code? read this!" link below the text area...)
S02 S02

2014/9/12

#
//Full Class public class Full extends Actor { Message m = new Message(); // Message is used to print message in world. public Full() { GreenfootImage image = getImage() ; image.scale( 150, 180 ) ; } protected void setMessage( String msg ) { int x, y ; m.setText( msg ); x=getX(); y=getY(); World world = getWorld(); if ( m.getWorld() != null ) { world.removeObject( m ) ; return; } world.addObject(m, X,Y ) ; } public class Half extends Full { public void update(Coin coin){ inspect(coin); } public void inspect( Coin coin ) { System.out.println( "Coin: " + coin.getClass().getName() ) ; setMessage(coin.getClass().getName()) ; } } /// I am calling Half using below code snippet: Half half = new Half(); half.update(coin); Getting null pointer exception in base class i.e Full class at line: X=getX(); I want to display a message in my world.
danpost danpost

2014/9/12

#
1
2
Half half = new Half();
half.update(coin);
With this, the first line creates a new Half object and the second calls 'update' on that object. 'update' in turn calls 'setMessage' on the Half object which will require the Half object be in the world (because it uses 'getX()' and 'getY()' ). But this new Half object was never added into the world; so, an exception is thrown.
S02 S02

2014/9/12

#
Whats the solution to this? How can i add Half object to my world?
danpost danpost

2014/9/12

#
I cannot say exactly what you should do or how you should add the Half object into the world. It depends on what class this snippet is in (what type -- Actor or World or other). Then, if it is an Actor, is that actor in the world or not. It might help to show the entire class where this snippet appears. And please use the 'code' link below the 'Post a reply' box to insert (copy/paste) your code into your posts.
S02 S02

2014/9/12

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import greenfoot.*; 
 
public class GumballMachine extends Actor
{
 Message m=new Message();
 Inspector inspector = new Inspector();
    Actor haveCoin;
     
    public GumballMachine()
    {
        GreenfootImage image = getImage() ;
        image.scale( 350, 400 ) ;
    }
     
 protected void setMessage( String msg )
    {
               
        int mouseX, mouseY ;
        MouseInfo mouse = Greenfoot.getMouseInfo();
        mouseX=mouse.getX();
        mouseY=mouse.getY();
        World world = getWorld();
          
        if ( m.getWorld() != null )
        {
            world.removeObject( m ) ;
        }
        world.addObject(m, mouseX, mouseY ) ;
        m.setText( msg ) ;
    }
     
    public void act()
    {
        if(Greenfoot.mousePressed(this)) { 
             if ( haveCoin == null ){
                setMessage( "No Coin Now!" ) ;
            }
            else
            
                setMessage( "Crank Turned!" ) ;
                  inspector.update((Coin)haveCoin);   // Calling inspector class
                haveCoin = null ;
            }
        }
 
        Actor coin = getOneObjectAtOffset(+10, +10, Coin.class ) ;
        if ( coin != null )
        {
           haveCoin=(Coin)coin;
            setMessage("Have Coin Now");
            World world = getWorld() ;
            world.removeObject( haveCoin ) ;
        }
    }   
}
 
 
 
///// Inspector Class////////////////////////
 
public class Inspector extends Alien
{
    
    public void addPicker(Picker obj) {
        pickers.add(obj) ;  
    }
 
    public  void removePicker(Picker obj) {
        System.out.println("Inside removePicker");
        pickers.remove(obj) ;
    }
 
   public void update(Coin coin){
        
       inspect(coin);
     }
     
    public void inspect( Coin coin )
    {
       
       setMessage(coin.getClass().getName()) ;// Calling method of base class
        
           
       }
        
       }
 
 
//// Alien Class/////
 
public class Alien extends Actor
{
Message m = new Message();
 
    public Alien()
    {
        GreenfootImage image = getImage() ;
        image.scale( 150, 180 ) ;
    }
 protected void setMessage( String msg )
    {
         
        int X, Y ;
        m.setText( msg );
        X=getX(); // i am getting error in this line
        Y=getY();
        World world = getWorld();
         
        if ( m.getWorld() != null )
        {
            world.removeObject( m ) ;
            return;
        }
        world.addObject(m, X,Y ) ;
             
    }
       
}
 
/// Message Class/////
import greenfoot.*; 
 
 
public class Message extends Actor
{
     
    GreenfootImage gi;
 
    public Message()
    {
        gi = new GreenfootImage( 100, 50);
        setImage(gi);       
    }
 
    public void setText( String msg )
    {
        gi.clear();
        gi.setColor( java.awt.Color.YELLOW ) ;
        gi.fill() ;
        gi.setColor( java.awt.Color.BLACK ) ;
        gi.drawString( msg, 0, 25 ); 
    }
    
 
    public void act()
    {
        if(Greenfoot.mousePressed(this)) {         
            World world = getWorld();
            world.removeObject( this ) ;
        }
    }       
}
danpost danpost

2014/9/12

#
The problem is you have too many Message objects being created. Each time you instantiate (create an object from) the Alien class, the Inspector class or the GumballMachine class, you are, in turn, creating a Message object (see line 5 and line 93 -- because Inspector extends Alien, an Inspector object is considered an Alien object and one is create there also -- I am not sure if you want the one to extend the other, anyway; if the inspector is not an alien, then Inspector should not extend Alien).
S02 S02

2014/9/12

#
Inspector is Alien. And i need all these Message object to print different messages on ever click and event in my world. I am getting Null Pointer exception while calling setMessage function from Inspector class. Please help me in resolving this
danpost danpost

2014/9/12

#
Do you want several messages to be displayed at once? or do you just want one display to show all messages?
S02 S02

2014/9/12

#
Yes i want several messages to display at once, at least 3 are displaying in one go. I need different messages at different location in world.
Super_Hippo Super_Hippo

2014/9/12

#
Maybe you should actually add the 'inspector' (line 6) to your world. You are trying to get the position of it in the world although it was never added. If it is added in your world class, then the 'inspector' in line 6 doesn't refer to the same object.
danpost danpost

2014/9/12

#
You must make sure your Inspector object is in the world before you call 'inspect' on it at line 41 (at least, to start with).
danpost danpost

2014/9/12

#
Super_Hippo wrote...
Maybe you should actually add the 'inspector' (line 6) to your world. You are trying to get the position of it in the world although it was never added. If it is added in your world class, then the 'inspector' in line 6 doesn't refer to the same object.
Well put!
There are more replies on the next page.
1
2