0

I am facing a problem where I have a null pointer exception, I am trying to work on a video compression program, this is the part where the exception is appearing:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileInputStream fis=null;
    File file=null;

    try
    {
        URL uri=CompressionTest.class.getResource("/Files/Video.mp4");

        file=new File(uri.getPath());

        fis = new FileInputStream(file);
    } 
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 1
    Check for nllptr before using the variables. – Murat Karagöz Mar 25 '15 at 15:06
  • 3
    It would help if you were a bit more specific about the locus of the problem, such as by providing a stack trace. From what you did provide, however, my best guess would be that `CompressionTest.class.getResource("/Files/Video.mp4")` is returning `null`, as it will do if no resource matching the given name is found. – John Bollinger Mar 25 '15 at 15:09
  • 2
    If you read the [javadoc](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)), you'll notice that `getResource()` returns "A URL object or **null if no resource with this name is found**". (emphasis mine) This is most likely what is causing your NPE, but in the future please post a complete stack trace when asking for help with runtime errors. – azurefrog Mar 25 '15 at 15:10
  • Which line exactly is the exception appearing in? What does it say? Edit your question with the full stack trace and we'll be able to help you a lot better :) – mhlz Mar 25 '15 at 15:10
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – yiabiten Mar 25 '15 at 15:12

1 Answers1

2

Check whether the uri is null before using it:

if (uri===null) {
    //Handle the situation, propably throw other exception
}
//rest of the code

Class.getResource might return null if the resource is not available (as stated in its documentation)

mhlz
  • 3,497
  • 2
  • 23
  • 35
novy1234
  • 333
  • 1
  • 11