当前位置: 首页 > 知识库问答 >
问题:

使用java的JSON中出错:主线程中出现异常[重复]

方焱
2023-03-14

我对JSON和java是新手,并尝试使用maven和eclipse来探索它
这是我的json文件。

  "name": "Jason Bourne",
  "profession": "Super agent",
  "bad-guy": false,
  "kills": 1000,
  "phoneNumbers": [
    {
      "type": "home",
      "number": "123-456-789"
    },
    {
      "type": "work",
      "number": "123-555-555"
    }
  ]
},
{
  "name": "Jason Voorhees",
  "profession": "Maniac killer",
  "bad-guy": true,
  "kills": 100,
  "phoneNumbers": [
    {
      "type": "office",
      "number": "666-666-666"
    }
  ]
},
{
  "name": "Jason",
  "profession": "Lead of Argonauts",
  "bad-guy": false,
  "kills": null
}
]  

我的pom.xml档案是这样的

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.assignment</groupId>
  <artifactId>assgQuestion</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jsonp.version>1.1.2</jsonp.version>
 </properties>
  
  <dependencies>
    <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.json/javax.json-api -->
<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>${jsonp.version}</version>
</dependency>

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>${jsonp.version}</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

  </dependencies>
  
  <build>


<plugins>


<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.7.0</version>


<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>

</plugin>


<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<version>1.6.0</version>


<configuration>

<executable>java</executable>


<arguments>

<argument>-classpath</argument>

<!-- automatically creates the classpath using all project dependencies,also adding the project build directory -->


<classpath/>

<argument>packt.javaee.json.Main</argument>

</arguments>

</configuration>

</plugin>

</plugins>

</build>
</project>  

我试图列出所有jsonp事件。下面是相同的代码段。

public class ReadJSONFile {
    
   public static void main(String args[]) {
       new ReadJSONFile().start();
   }
   
   private void start() {
       while(true) {
           printOptions();
           
           Scanner sc = new Scanner(System.in);
           switch(sc.nextLine()){
                case "1" : 
                jsonParserScenario();
                break;
           }    
                
          }
       }
   
   
        private void printOptions() {
            System.out.println("1. JSON Parser");
            
        }
        
        private void jsonParserScenario() {
          //Creating a JSONParser object
            JsonParser parser = Json.createParser(ReadJSONFile.class.getResourceAsStream("/person.json"));
            
            while (parser.hasNext()) {
                
                //Get event from parse
                JsonParser.Event event = parser.next();
                
                //Display event on screen
                System.out.println(event.toString());
            }
       }
}  

运行上述文件后,我得到以下错误。

1. JSON Parser
1
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read()" because "this.in" is null
    at org.glassfish.json.UnicodeDetectingInputStream.fillBuf(UnicodeDetectingInputStream.java:89)
    at org.glassfish.json.UnicodeDetectingInputStream.detectEncoding(UnicodeDetectingInputStream.java:128)
    at org.glassfish.json.UnicodeDetectingInputStream.<init>(UnicodeDetectingInputStream.java:75)
    at org.glassfish.json.JsonParserImpl.<init>(JsonParserImpl.java:95)
    at org.glassfish.json.JsonProviderImpl.createParser(JsonProviderImpl.java:88)
    at javax.json.Json.createParser(Json.java:109)
    at assgQuestion.ReadJSONFile.jsonParserScenario(ReadJSONFile.java:52)
    at assgQuestion.ReadJSONFile.start(ReadJSONFile.java:29)
    at assgQuestion.ReadJSONFile.main(ReadJSONFile.java:19)  

有人能帮我找出哪里出了问题吗。

共有1个答案

孟思远
2023-03-14

问题(一个NullPointerException)发生在这一行的createParser调用中:

JsonParser parser = Json.createParser(
    ReadJSONFile.class.getResourceAsStream("/person.json"));

这是因为高速公路

ReadJSONFile.class.getResourceAsStream("/person.json")

正在计算为null

如果您查看类的javadoc。getResourceAsStream,您将看到当类类加载器在类加载器的命名空间中找不到命名资源时,将返回null

因为您使用的是Maven,所以它要查找的文件需要位于项目的“src/main/resources”目录中;有关详细信息,请参见标准目录布局的介绍。

最好将JSON文件放在文件系统中,并使用FileInputStream打开它。在这里使用资源的问题是,您的应用程序只能读取嵌入在应用程序中的JSON文件;e、 在它的JAR文件中。

 类似资料: