• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlet calling an applet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am displaying the downline tree of the members using JTree. This program takes the id of the member and generates his downline.the problem is it is showing only the servlet but the applet is not coming. why is it so. i am not able to call this applet directly from html file since it gives nullpointerexception error.i am using servlet since i have to retrieve id of the member from the database. please provide me a solution for this as soon as possible.if there is any other method please give the source code.
thanks in advance
// AccountMember.java
import java.util.*;
public class AccountMember
{
int id;
String fname,lname,city;
HashSet hs; // can use TreeSet instead of HashSet but Hashset provides faster accessing

public AccountMember(int i,String name,String name1,String city1) {
id = i;
fname = name;
lname = name1;
city = city1;
hs = new HashSet();
}

public void addMem(AccountMember newmem) {
hs.add(newmem);
}


int getId() { return id; }

String getFname() { return fname; }

String getLname() { return lname; }

String getCity() { return city; }

HashSet getChildSet() { return hs; }

public String toString() { return id + "," + " " + fname + " " + lname + "," + " " + city ; }

boolean hasChild() {
if( hs.size() == 0 )
return false;
else return true;

}
}
// MLM.java
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
public class MLM extends JApplet
{
AccountMember topmost;
DefaultMutableTreeNode rootNode;
DefaultTreeModel treeModel;
JTree tree;
JScrollPane scrollPane;
Container cp;

public void init() {
rootNode = new DefaultMutableTreeNode(topmost);
treeModel = new DefaultTreeModel(rootNode);
tree = new JTree(treeModel);
tree.putClientProperty("JTree.lineStyle", "Angled");
scrollPane = new JScrollPane(tree);
cp = getContentPane();
cp.add(scrollPane);
}
public void start() {
insertMemNode(topmost, rootNode);
}
public void assignRoot(AccountMember acmem) {
topmost = acmem;
}
void insertMemNode(AccountMember parent, DefaultMutableTreeNode parentNode) {
Iterator itr = parent.hs.iterator();
while(itr.hasNext()) {
AccountMember child = (AccountMember)itr.next();
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
treeModel.insertNodeInto(childNode, parentNode, 0);
insertMemNode(child, childNode);
}
}
}
// CheckServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.*;
public class CheckServlet extends HttpServlet
{
Connection con = null;
ResultSet rs = null;
Statement st = null;
int ref1;
String fname2,lname2,city2;
String fname,lname,city;
AccountMember root;
AccountMember parent;
String fname1;
MLM mlm;
int level;
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();

String uid1 = req.getParameter("username3");
String pwd1 = req.getParameter("password3");
String ref = req.getParameter("refno");
ref1 = Integer.parseInt(ref);


out.println("</embed> </object></div></td></tr></table><br>");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("Jdbc dbc:mark");
st = con.createStatement();
ResultSet rs2 = st.executeQuery("select * from paisapayment where accountno='" + ref1 + "'");
while (rs2.next())
{
fname2 = rs2.getString(3);
lname2 = rs2.getString(4);
city2 = rs2.getString(11);

}

mlm = new MLM();
root = new AccountMember(ref1,fname2,lname2,city2);
ret(root);
mlm.assignRoot(root);

}//try
catch(Exception q)
{ q.printStackTrace(); }
//calling an applet from servlet
out.println("<applet codebase = http://localhost/ code = MLM.class width=300 height=300></applet>");

out.println("</form></b></body></html>");
} // End of doPost()
public void ret(AccountMember parent)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("Jdbc dbc:mark);
st = con.createStatement();
int id = parent.getId();
fname = parent.getFname();
lname = parent.getLname();
city = parent.getCity();
ResultSet rs1 = st.executeQuery("select * from paisapayment where referedby='" + id + "' order by accountno");
while (rs1.next())
{
AccountMember child = new AccountMember(rs1.getInt(1),rs1.getString(3),rs1.getString(4),rs1.getString(11));
parent.addMem(child);
ret(child);


}

}//try
catch(Exception q)
{
q.printStackTrace();
}
}// End of ret(AccountMember)



}
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A general idea is if you are getting null pointer exception, it may be connection error or applet creation error. try to capture the errors using exception, say while calling servlet,while initialling applet,while sending back data, etc.,
 
Ranch Hand
Posts: 250
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mala H.R.:
I am not able to call this applet directly from html file since it gives nullpointerexception error.


Apropos your above statement I looked at the Applet code and it seems you're getting this error beacuse you have not created the AccountMember object "topmost" before using it inside insertMemNode(). Insert a line similar to following in the init() and applet will surely run.

Do tell me if this worked.
 
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic