import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import…
- User
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken;
import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Type; import java.util.List; import java.util.ArrayList;
public class HOTELIERServer {
private List<Hotel> hotels; public HOTELIERServer(String jsonFilePath) { loadHotelsFromJson(jsonFilePath); } private void loadHotelsFromJson(String jsonFilePath) { Gson gson = new Gson(); try (FileReader reader = new FileReader(jsonFilePath)) { // Defining the type of the data we want to read Type hotelListType = new TypeToken<ArrayList<Hotel>>(){}.getType(); // Deserializing hotels = gson.fromJson(reader, hotelListType); } catch (IOException e) { e.printStackTrace(); } } // Add other functionality as needed for the HOTELIERServer public static void main(String[] args) { String jsonFilePath = "Hotels.json"; // Replace with the actual path to the JSON file HOTELIERServer server = new HOTELIERServer(jsonFilePath); // You can use the instance server to interact with the list of hotels loaded from the JSON file // Example: Print all hotel names for (Hotel hotel : server.hotels) { System.out.println(hotel.name); } }}
perchè mi da questo errore java.io.FileNotFoundException: Hotels.json (Impossibile trovare il file specificato) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:213) at java.base/java.io.FileInputStream.(FileInputStream.java:152) at java.base/java.io.FileInputStream.(FileInputStream.java:106) at java.base/java.io.FileReader.(FileReader.java:60) at esame/esame.HOTELIERServer.loadHotelsFromJson(HOTELIERServer.java:22) at esame/esame.HOTELIERServer.(HOTELIERServer.java:17) at esame/esame.HOTELIERServer.main(HOTELIERServer.java:36) Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "server.hotels" is null
- Assistant
L'errore
java.io.FileNotFoundException: Hotels.json (Impossibile trovare il file specificato)indica che il programma sta cercando di aprire un file chiamatoHotels.json, ma non riesce a trovarlo nel percorso specificato. Questo è spesso dovuto al fatto che il percorso del file non è corretto o il file non si trova nella directory da cui si esegue il programma.Per risolvere questo problema:
- Verifica che il file
Hotels.jsonesista davvero nella directory del tuo progetto o nel percorso da cui viene eseguito il programma. - Se il file non è nella stessa directory del programma, fornisce un percorso completo o relativo al file. Un esempio del percorso relativo potrebbe essere se il file si trova nella cartella sotto la cartella di un progetto Maven o Gradle.
- Verifica che il file
- User
Ora mi dà questo errore
Exception in thread "main" com.google.gson.JsonIOException: Failed making field 'esame.Hotel#id' accessible; either increase its visibility or write a custom TypeAdapter for its declaring type. at com.google.gson@2.10.1/com.google.gson.internal.reflect.ReflectionHelper.makeAccessible(ReflectionHelper.java:38) at com.google.gson@2.10.1/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:286) at com.google.gson@2.10.1/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:130) at com.google.gson@2.10.1/com.google.gson.Gson.getAdapter(Gson.java:556) at com.google.gson@2.10.1/com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53) at com.google.gson@2.10.1/com.google.gson.Gson.getAdapter(Gson.java:556) at com.google.gson@2.10.1/com.google.gson.Gson.fromJson(Gson.java:1226) at com.google.gson@2.10.1/com.google.gson.Gson.fromJson(Gson.java:1137) at com.google.gson@2.10.1/com.google.gson.Gson.fromJson(Gson.java:1107) at esame/esame.HOTELIERServer.loadHotelsFromJson(HOTELIERServer.java:26) at esame/esame.HOTELIERServer.(HOTELIERServer.java:17) at esame/esame.HOTELIERServer.main(HOTELIERServer.java:36) Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field int esame.Hotel.id accessible: module esame does not "opens esame" to module com.google.gson at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:391) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:367) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:315) at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:183) at java.base/java.lang.reflect.Field.setAccessible(Field.java:177) at com.google.gson@2.10.1/com.google.gson.internal.reflect.ReflectionHelper.makeAccessible(ReflectionHelper.java:35) ... 11 more
- Assistant
L'eccezione
com.google.gson.JsonIOExceptioninsieme all'errore sottostantejava.lang.reflect.InaccessibleObjectExceptionsuggerisce che esiste un problema di accessibilità ai campi della classeHotel. Da Java 9 in poi, con l'introduzione del sistema di moduli, esistono regole più stringenti sull'accessibilità dei membri delle classi.Nel tuo caso, stai cercando di deserializzare un oggetto JSON in istanze della classe usando Gson, che tenta di accedere ai campi privati della classe , e questo fallisce perché la visibilità dei membri della classe è limitata.