那是我的控制器:
public class GreetingController implements Controller
{
private static final String MARKERS_FILE_NAME = "markers.txt";
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
String result = null;
File file = new File(getClass().getResource(MARKERS_FILE_NAME).toURI());
}
}
我的文件markers.txt 与控制器处于同一级别,但令人费解的是,我在这一行得到了一个Nullpointer:File file = new File(getClass().getResource(MARKERS_FILE_NAME).toURI());
java.lang.NullPointerException
com.codenvy.example.spring.GreetingController.handleRequest(GreetingController.java:27)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
仅供参考:我正在使用https://codenvy.com/
我也试过
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("com/codenvy/example/spring/markers.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
有了这个结果:
java.lang.NullPointerException
java.io.Reader.<init>(Reader.java:78)
java.io.InputStreamReader.<init>(InputStreamReader.java:72)
com.codenvy.example.spring.GreetingController.handleRequest(GreetingController.java:32)