Skip to Content
avatar image
Former Member

Throwing Null Pointer Exception

Hi,

I developed a program J2EE Web Module Project. In which when i enter a vendor number it has to get the details of the particular Vendor Name.

But when i deployed it is throwing an exception "NullPointerException". But I could not recognize it. Please let me know where is the error.

Here is my HTML and Servlet Code ZTEST_VENDOR is my BAPI (RFC):

<html>

<head>

<title> Vendor Details</title>

<script language="JavaScript">

function randomnumber()

{

var r=Math.floor(Math.random()*1111);

if ( r!=0)

{

document.form1.rand.value=r;

}

}

function getVendorDetails()

{

document.location = "http://localhost:8880/vendor/Servlet";

}

</script>

</head>

<body bgcolor="#eeeff8" onLoad = "javascript:randomnumber();">

<center>

<hr>

<h1>Enter the Vendor Number</h1>

<hr>

</center>

<form name=form1 action="Servlet" method=post>

<center>

<input type="text" name="vendno">

<input type="Submit" value="Submit">

<input type=hidden name="rand">

</center>

</form>

</body>

</html>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

</BODY>

</HTML>

and Servlet code is as below :

package vendor.pkg;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.sap.mw.jco.IRepository;

import com.sap.mw.jco.JCO;

import com.sap.mw.jco.IFunctionTemplate;

public class VendorServlet extends HttpServlet {

PrintWriter pw;

public void doPost(HttpServletRequest req, HttpServletResponse res) {

int num = Integer.parseInt(req.getParameter("rand"));

String no, name, city, po;

String SID = "R" + num;

String vendno = req.getParameter("vendno");

IRepository repository;

// The repository we will be using

try {

// Add a connection pool to the specified system

JCO.addClientPool(

SID,

100,

"200",

"pavan",

"pavan",

"EN",

"sapdev",

"00");

repository = JCO.createRepository("MYRepository", SID);

pw.println("Connection got");

IFunctionTemplate ftemplate =

repository.getFunctionTemplate("ZTEST_VENDOR");

JCO.Function function = new JCO.Function(ftemplate);

JCO.Client client = JCO.getClient(SID);

JCO.ParameterList input = function.getImportParameterList();

input.setValue(vendno, "LIFNR");

client.execute(function);

JCO.Structure ret =

function.getExportParameterList().getStructure("RETURN");

pw = res.getWriter();

pw.println(

"<html><head><title>VendorDetails</title></head><body bgcolor=#eeeff8><center><hr><h1>Customer Details</h1><hr>");

JCO.Table vend = function.getTableParameterList().getTable("ITAB");

for (int i = 0; i < vend.getNumRows(); i++) {

vend.setRow(i);

no = vend.getString("LIFNR");

name = vend.getString("NAME1");

city = vend.getString("ORT01");

po = vend.getString("PFACH");

pw.println(

"<table border=1><tr><td><B>Vendor Number</B></td><td>"

+ no

+ "</td></tr><tr><td>"

+ "<B>Customer Name</B></td><td>"

+ name

+ "</td></tr><tr><td>"

+ "<B>Customer Address</B></td><td></tr>"

+ "<tr><td> </td><td><B>City</B></td><td>"

+ city

+ "</td></tr>"

+ "<tr><td> </td><td><B>District</B></td><td>"

+ po

+ "</td></tr>+</table>");

pw.println(

"<form name=form1 action='Index.html' method=get><input type=submit value='Back'></form></center></body></html>");

}

} catch (Exception E) {

pw.println(E);

}

}

}

Please help me out.

Thanks,

Pavan.

Message was edited by: Pavan Panduru

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    May 15, 2006 at 03:51 AM

    pw.println("Connection got");

    is the culprit.:) you have initialized pw after this statement is executed and hence the error

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 15, 2006 at 04:17 AM

    hi

    Are u getting the NullpointerException on deployment or

    while running the program .Also as amol had mentioned would be better if you could check the lof file sa nd post the stackTrace here

    hope this helps, please mark helpful answers

    regards

    rajeshkr

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Pavan,

      It seems like you have not intialized the JCo pool, to do that do the following initialization before adding the client to the pool.

      JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);

      The full code should look like this,

      try {

      JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);

      if (pool == null) {

      OrderedProperties logonProperties =

      OrderedProperties.load("/logon.properties");

      JCO.addClientPool(POOL_NAME, // pool name

      5, // maximum number of connections

      logonProperties); // properties

      }

      mConnection = JCO.getClient(POOL_NAME);

      System.out.println(mConnection.getAttributes());

      }

      catch (Exception ex) {

      ex.printStackTrace();

      }

      finally {

      JCO.releaseClient(mConnection);

      }

      Since there was no pool it was giving a null pointer axception. For a detailed tutorial go to the following link,

      https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/fb723cce-0a01-0010-2393-fbb6c5dc7ff4

      I hope this will certailny help you.

      Best regards,

      Guru.

      PS: Don't forget the points 😊