Home Java Java-get-example Java error java.lang.nullpointerexception
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java error java.lang.nullpointerexception

Advertisement
NullPointerException is the exception that occurred when a programmer perform an operation on a object or calling a method on the object i.e null. This exception is thrown by JVM.

Java error java.lang.nullpointerexception

     

NullPointerException  is the exception that occurred when a programmer perform an operation on a object  or calling a method on the object i.e null. This exception is thrown by JVM.We should not handle that exception by try and catch block  The only way to resolve Null Pointer Exception is to avoid performing the operation on an object or method of the object i.e is null.

Understand with Example

In this Tutorial we want to describe you a code that help you in understanding a Java Null pointer Exception. For this we have a class javaerrornullpointerexception.Inside this class we have a main method that declare a String array to hold the dimensional size of 5.

string.charAt( )-This method return you the position of the character specified by the  index of the parameter.

On the execution of the code ,the program code show us an error i.e java.lang .Null Pointer Exception because  we have not initialized the value of string array.

javaerrornullpointerexception.java


public class javaerrornullpointerexception {

  public static void main(String args[]) {
  String string[] new String[5];  
  System.out.println(string[1].charAt(1));

  }
}

Output

Compiling source file to /home/girish/NetBeansProjects/errors/build/classes
compile-single:
run-single:
Exception in thread "main" java.lang.NullPointerException
  at javaerrornullpointerexception.main(javaerrornullpointerexception.java:8)
Java Result: 1
BUILD SUCCESSFUL (total time: seconds)

To resolve this error you have to initialise array as shown string[1]="dgdsf"

Download code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 3, 2008

Ask Questions?    Discuss: Java error java.lang.nullpointerexception   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
sonoo maurya
May 29, 2011
java

i want know, why we are use final for exception handling?
k.karthikeyan
May 26, 2013
online purchase

none
k.karthikeyan
May 26, 2013
online purchase

nullpointerexception
saiviswanath
February 4, 2013
nullpointer download

i want this game and i want this null pointer
sangeetha
November 23, 2011
Java - How to handle null pointer exception?

How to handle null pointer exception?
yehuda
December 25, 2011
java arrays null pointer error

Exception in thread "main" java.lang.NullPointerException at java.util.Arrays.mergeSort(Arrays.java:1157) at java.util.Arrays.sort(Arrays.java:1092) at begin.main(begin.java:44) the error only comes after i sort them. Arrays.sort(array3); but the same arrays runs fine up until the sorted one help thanks
agnes
January 19, 2013
help

hi im having 1 prog,in this when i try to insert data from datatable to database,it showing me null pointer exception plz give me some suggestion thank u
Marius Daniel
March 6, 2012
java.lang.nullpointer.exception

tento program je nefunkcny a sposobuje trvale alebo docasne nasledky? ja som mal hru Minecraft a na nej my tento program chyba ja si ho potrebujem stiahnut ale este stale som nenasiel stranku na ktorej sa to da stiahnut tento subor a este vam chcem doporucit ze ak tento program umoznite stiahnut tak si aj zarobite lebo vela ludi tento program potrebuje, dakujem!
manish kumar gupta
April 9, 2012
lic data

good
MARIO COSTA
April 11, 2012
TELEPHONE(blackberry )

GOOD DAY, I'M NOT ABLE TO RECEIVE MESSAGE. I DON'T KNOW WHAT IS THIS:UNCAUGHT EXCEPTION JAVA.LANG.NULLPOINTEREXCEPTION.
dilrajkaur
May 13, 2012
java.lang.nullpointer exception

i got this message on bbm and bbm stopped working, what can i do to make it work again
GANESH K
June 4, 2012
nullpointerexception

thank you
madhu
July 5, 2012
String class

how can i print the reverse of a string?I tried with reverse method but could not succeed. thanks for your help
Rushi Chaudhari
May 5, 2013
null pointer exception

Sir im getting the same null pointer exception plz help import java.io.*; import java.util.*; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Cell; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; class bentupdate { static String [][] excelData; static int nop=0,advamt,balamt,amt; static String cus="",pl="",typ="",db="",dd=""; public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter name of customer"); cus=sc.next(); System.out.println("Enter place"); pl=sc.next(); System.out.println("Enter type ofplant"); typ=sc.next(); System.out.println("Enter date of booking in dd/mm/yy"); db=sc.next(); System.out.println("Enter date of delivery in dd/mm/yy"); dd=sc.next(); System.out.println("Enter no. of plants"); nop=sc.nextInt(); System.out.println("Enter amt"); amt=sc.nextInt(); System.out.println("Enter advamce amt"); advamt=sc.nextInt(); System.out.println("Enter Balance amt"); balamt=sc.nextInt(); // -------------------------------------------------------------------------- try { FileInputStream file = new FileInputStream(new File("C:\\temp\\testPOIWrite.xls")); HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = workbook.getSheetAt(0); Cell cell=null; int x=1,y=0; for(x=1;x<200;x++) { cell=sheet.getRow(x).getCell(0); if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { //Update the value of cell sheet.getRow(x).getCell(0).setCellValue(x); cell = sheet.getRow(x).getCell(0); cell.setCellValue(x); cell = sheet.getRow(x).getCell(1); cell.setCellValue(cus); cell = sheet.getRow(x).getCell(2); cell.setCellValue(pl); cell = sheet.getRow(x).getCell(3); cell.setCellValue(typ); cell = sheet.getRow(x).getCell(4); cell.setCellValue(db); cell = sheet.getRow(x).getCell(5); cell.setCellValue(nop); cell = sheet.getRow(x).getCell(6); cell.setCellValue(amt); cell = sheet.getRow(x).getCell(7); cell.setCellValue(advamt); cell = sheet.getRow(x).getCell(8); cell.setCellValue(balamt); cell = sheet.getRow(x).getCell(9); cell.setCellValue(dd); FileOutputStream outFile =new FileOutputStream(new File("C:\\temp\\testPOIWrite.xls")); workbook.write(outFile); outFile.close(); file.close(); System.exit(0); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } } }
DMCA.com