I have a DrawingView
class that extends the View
class. In that DrawingView
class I have a websocket client defined, where the client recieves a String and divides it into parts where each part will be inserted into an MotionEvent
object. Then that object is sent via an onTouchEvent
function in the same class and is handled later. So my problem is that the recieved_event
is null when using it. Also if I define it locally instead of globally, Android Studio argues that the Object should be set as null. Is there any way to use the recieved_event
without getting the null error?
public class DrawingView extends View{
MotionEvent recieved_event;
public DrawingView(Context context, AttributeSet attr){
super(context,attr);
//the client socket is started
}
private final class EchoWebSocketListener extends WebSocketListener {
private static final int NORMAL_CLOSURE_STATUS = 1000;
@Override
public void onOpen(WebSocket webSocket, Response response) {
webSocket.send("Hello!");
}
@Override
public void onMessage(WebSocket webSocket, String text) {
if(text.contains("!!!!")){
String[] event_attrs = text.split("!!!!");
//The recieved_event is null
recieved_event.setLocation(Float.parseFloat(event_attrs[1]), Float.parseFloat(event_attrs[2]));
recieved_event.setAction(Integer.parseInt(event_attrs[0]));
onTouchEvent(recieved_event);
}
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(NORMAL_CLOSURE_STATUS, null);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
}
}
}
The error I get:
Attempt to invoke virtual method 'void android.view.MotionEvent.setLocation(float, float)' on a null object reference