473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

What is problem with this code.It gives me NullPointerException and i am very new to

This is my Test.java class
package com.durgasoft.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.durgasoft.pojo.Employee;

public class Test {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
SessionFactory sf = null;
Session s = null;
try {
Configuration cfg=new Configuration();
cfg.configure("/com/durgasoft/resources/hibernate.cfg.xml");
sf=cfg.buildSessionFactory();
s=sf.openSession();
Employee emp=(Employee) s.load(Employee.class, 111);
if (emp==null) {
System.out.println("Employee not Existed");
}
else{
System.out.println("Employee Details");
System.out.println("------------------");
System.out.println("Employee No:"+emp.getEno());
System.out.println("Employee Name:"+emp.getEname());
System.out.println("Employee Salary:"+emp.getEsal());
System.out.println("Employee Address:"+emp.getEaddr());
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
s.close();
sf.close();

}

}

}
================================================== =======

Employee.java
package com.durgasoft.pojo;

public class Employee {
private int eno;
private String ename;
private float esal;
private String eaddr;
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public float getEsal() {
return esal;
}
public void setEsal(float esal) {
this.esal = esal;
}
public String getEaddr() {
return eaddr;
}
public void setEaddr(String eaddr) {
this.eaddr = eaddr;
}

}
================================================== =======
Employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.durgasoft.pojo.Employee" table="emp1"> <id name="eno"/> <property name="ename"/> <property name="esal"/> <property name="eaddr"/> </class> </hibernate-mapping>
================================================== =======
hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">oracle.jd bc.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@ localhost:1521:xe</property> <property name="hibernate.connection.username">mydb</property> <property name="hibernate.connection.password">mydb</property> <property name="show_sql">true</property> <property name="hibernate.dialect">org.hibernate.dialect.Ora cleDialect</property> <mapping resource="com/durgasoft/resources/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
Oct 1 '18 #1
1 2111
Ishan Shah
47 32bit
The java.lang.NullPointerException is thrown when a reference variable is accessed or de-referenced and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

Following code will solve your problem statement :

Expand|Select|Wrap|Line Numbers
  1. catch (NullPointerException e)
  2. {
  3.        e.printStackTrace();
  4. }
  5.  
Sep 25 '20 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: RobertTG | last post by:
Someone please translate the code below into English... Particularly the indicated line Thanks function attachComment() { var aForms = document.getElementsByTagName("FORM"); for (var i = 0;...
13
by: dbuchanan | last post by:
This code resets a form with two cbo's (comboBoxes) and one datagrid. The first cbo (cboSelection) selects a main table and filters the second cbo. The second cbo (cboView) selects the secondary...
9
by: oddvark | last post by:
Hello, under vc7.1 this code compiles: if (!parent.fillTool) parent.fillTool.dispose; where dispose is a method of fillTool. Notice that dispose does not have ( ) behind it. Under vc8,...
1
by: locy | last post by:
can someone explain to me "what does this code do" class baseclass { public: virtual void runme() { std::cout<<"are you"<<std::endl; }
5
by: lucas | last post by:
is a javascript file; is ajax? or javascript but in hidden code? eval(function(p,a,c,k,e,d) {e=function(c)...
3
by: qianz99 | last post by:
Hi I am not sure what this code does. I have the following questions 1. where is the case? 2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a function TLV_INTEGER(name, octets) ...
6
by: sbcs | last post by:
I'm a website developer. Recently I've found variations of this code on the home pages of several of my sites. It triggers warnings in some anti-virus/malware programs but not in others. The pages...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.