Błąd:
Exception in thread "Thread-5" java.lang.NullPointerException
at com.lynden.gmapsfx.javascript.JavascriptRuntime.execute(JavascriptRuntime.java:63)
at com.lynden.gmapsfx.javascript.JavascriptObject.<init>(JavascriptObject.java:76)
at com.lynden.gmapsfx.javascript.object.LatLong.<init>(LatLong.java:36)
at thread.run(thread.java:32)
at java.lang.Thread.run(Unknown Source)
Kod źródłowy:
import com.lynden.gmapsfx.GoogleMapView;
import com.lynden.gmapsfx.MapComponentInitializedListener;
import com.lynden.gmapsfx.javascript.object.GoogleMap;
import com.lynden.gmapsfx.javascript.object.LatLong;
import com.lynden.gmapsfx.javascript.object.MapOptions;
import com.lynden.gmapsfx.javascript.object.MapTypeIdEnum;
import com.lynden.gmapsfx.javascript.object.Marker;
import com.lynden.gmapsfx.javascript.object.MarkerOptions;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.lang.NullPointerException;
public class Server extends Application implements MapComponentInitializedListener {
public thread manager;
public GoogleMapView mapView;
public GoogleMap map;
private MapOptions mapOptions;
protected static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// Class
manager = new thread(this);
// Thread
Thread newThread = new Thread(manager);
newThread.start();
mapView = new GoogleMapView();
mapView.addMapInializedListener(this);
Scene scene = new Scene(mapView);
stage.setTitle("Server - View");
stage.setScene(scene);
stage.show();
}
@Override
public void mapInitialized() {
MapOptions mapOptions = new MapOptions();
mapOptions.center(new LatLong(47.6097, -122.3331))
.mapType(MapTypeIdEnum.ROADMAP)
.overviewMapControl(false)
.panControl(false)
.rotateControl(true)
.scaleControl(false)
.streetViewControl(false)
.zoomControl(true)
.zoom(12);
map = mapView.createMap(mapOptions);
}
public boolean addEvent(int packetID, LatLong crd) {
try {
Marker marker = new Marker(new MarkerOptions()
.position(crd)
.title(new Integer(packetID).toString()));
map.addMarker(marker);
map.setCenter(crd);
return true;
} catch (NullPointerException error) {
System.out.println(error);
}
return false;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import com.lynden.gmapsfx.MapComponentInitializedListener;
import com.lynden.gmapsfx.javascript.object.LatLong;
import javafx.application.Application;
public class thread extends Thread {
protected static String networkCode = "zaqRTY!@AFGZq";
private Server server;
protected thread(Server serverPointer) {
server = serverPointer;
}
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(26890);
System.out.println("======= Server loaded =======");
System.out.println("IP:" + InetAddress.getLocalHost().getHostAddress());
System.out.println("Port:" + serverSocket.getLocalPort());
System.out.println(server.addEvent(141, new LatLong(241.4, 4.4))); ///////// TU JEST BLAD
while (true) {
Socket clientSocket = serverSocket.accept();
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Connection:" + clientSocket.getInetAddress().getLocalHost().getHostAddress());
String password = inputReader.readLine();
if(networkCode.equals(password)) {
try {
int packet = Integer.parseInt(inputReader.readLine());
double longitude = Double.parseDouble(inputReader.readLine());
double latitude = Double.parseDouble(inputReader.readLine());
System.out.println("Event:" + packet);
System.out.println("Longitude:" + longitude);
System.out.println("Latitude:" + latitude);
} catch(NumberFormatException error) {
System.out.println(error.getMessage());
}
} else if(password == null) {
System.out.println("Socket null, IP lost/end connection:" + clientSocket.getInetAddress().getLocalHost().getHostAddress());
}
} catch (IOException error) {
System.out.println(error.getMessage());
} finally {
try {
clientSocket.close();
} catch (IOException error) {
System.out.println(error.getMessage());
}
}
}
} catch (IOException error) {
System.out.println(error.getMessage());
}
}
}
Chciałem sobie zrobić prostą aplikacje z dwoma wątkami jeden na mapę, jeden na na sieć. Jak próbuję się odwołać z drugiego wątku do metody addMarker to wyrzuca mi właśnie błąd z początku tematu. Podejrzewam, że błąd leży w synchronizacji wątków?