-1

I am not familiar with java environments but I have to develop a java project and I found JPA that as I understand a technology like entity framework of microsoft.

I went through below tutorial at youtube but I am taking a null pointer exception but I couldnt understand whats happening because I did same things with tutorial. My project Structure is below. I have a jsp file and I am using this file as main page so I am running the project from this file. I included index.xhtml because of necessity. Also you can find this file and index2.java file(Its "JSF Managed Bean") contents below.

Problem is at "tablo1FacadeLocal.create(tbl1);" line.

If you can spot the problem it would be great but if you suggest another way or thing like entity framework it works me too.

http://www.youtube.com/watch?v=4HhRF20-Jhs

enter image description here

Tablo1 and Tablo2 are my tables so I created Tablo1.java and Tablo2.java "Entity classes from database".

Then I did new>"Session Beans for Entity Classes" and created facades in ejb package.

Finally I did new>"JSF Managed Bean". Managed Bean name is index2.java

index2.jsp

<%@page import="Web.index2"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World!</h1>

    <%
        index2 indx = new index2();
        indx.setTablo1Values();
    %>

</body>
</html>

index2.java

package Web;

import ejb.Tablo1FacadeLocal;
import entities.Tablo1;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class index2 {

    @EJB
    Tablo1FacadeLocal tablo1FacadeLocal;

    public index2() {
    }

    public void setTablo1Values() {
        Tablo1 tbl1 = new Tablo1();

        tbl1.setClm1(5);
        tbl1.setClmn2("fdfsfsdfs");

        tablo1FacadeLocal.create(tbl1);
    }
}
perissf
  • 15,979
  • 14
  • 80
  • 117
Yavuz
  • 1,257
  • 1
  • 16
  • 32
  • Sorry, I have been mislead by the missing indentations in your code. Why are you learning a deprecated technology like jsp? It has been replaced years ago by jsf/facelets. – perissf Apr 25 '14 at 18:13
  • so no actual mention of where the NPE is, what line? – Neil Stockton Apr 26 '14 at 09:34
  • @perissf I am not happy with this situation but I have to use jsp in my project. – Yavuz Apr 26 '14 at 09:56
  • @Neil Stockton this is the line: "tablo1FacadeLocal.create(tbl1);" – Yavuz Apr 26 '14 at 09:57
  • so tablo1FacadeLocal is null and you can conclude it is nothing to do with JPA. – Neil Stockton Apr 26 '14 at 10:13
  • Yes I know tablo1FacadeLocal is null. What I dont know is that it works at youtube tutorial I gave above but mine. And I am trying to find a solution for that. – Yavuz Apr 26 '14 at 10:23

1 Answers1

0

There are 2 options for you. The first is

try
{
    index2 indx=(index2)new   
    InitialContext().lookup(index2.class.getName());
}
catch(NamingException e)
{
    out.println(e.getMessage());
}

indx.setTablo1Values();

In the above you are treating index2 like an EJB which it is. When a bean is an EJB you cannot create an instance of the same. You need the container (server) to create it for you.
The 2nd option is to use JSP tags to access is hence
#{index2.setTablo1Values()}
I think the approach 2 might give you some issue for more information read here

Community
  • 1
  • 1
AdityaKeyal
  • 1,208
  • 8
  • 14