Hello all

can anybody tell me why i am getting null pointer exception.

String mesg="";
                try
                {
                        URL u = new URL(strOp);
                        BufferedReader r = new BufferedReader(new InputStreamReader( u.openStream()));
                        String s="";
                        while((s=r.readLine())!=null)
                        System.out.println(s);
                        if(s.equals("SUCCESS"))
                        mesg = mesg+"Your Request has been Processed.you will be intimated by a sms soon";
                        else
                        mesg = mesg.concat("Service is not available.Please try later");
                }

when i print the value of s it is giving "SUCCESS".But when assigning the string to mesg it is giving nullpointerexception

Is it "giving" "SUCCESS" or " SUCCESS" or "SUCCESS " or .... I think you get the idea. The only lines after the print that will result in an NPE are the s.equals..., and since s contained a value, seemingly, the line mesg = mesg.concat... is throwing the NPE. So, s does not match exactly "SUCCESS" or some code is missing. call "trim" on s in that equals call.

Is it "giving" "SUCCESS" or " SUCCESS" or "SUCCESS " or .... I think you get the idea. The only lines after the print that will result in an NPE are the s.equals..., and since s contained a value, seemingly, the line mesg = mesg.concat... is throwing the NPE. So, s does not match exactly "SUCCESS" or some code is missing. call "trim" on s in that equals call.

i used (s.trim().equals("SUCCESS")) but it is still giving null pointer exception

Is it "giving" "SUCCESS" or " SUCCESS" or "SUCCESS " or .... I think you get the idea. The only lines after the print that will result in an NPE are the s.equals..., and since s contained a value, seemingly, the line mesg = mesg.concat... is throwing the NPE. So, s does not match exactly "SUCCESS" or some code is missing. call "trim" on s in that equals call.

i got it buddy .s really contains the null.because i have the while loop which only exit when s becomes null.and i am using that value.

Fix your code indentation. It's no wonder I missed that while loop since I didn't bother looking at anything higher than the print statement that was on the same "level" as the print statement.

manish- read this
Remember, make it easy for people to help you out. Style matters!

manish- read this
Remember, make it easy for people to help you out. Style matters!

Thanks jon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.