jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes servlet calling an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet calling an applet" Watch "servlet calling an applet" New topic
Author

servlet calling an applet

mala H.R.
Greenhorn

Joined: Dec 21, 2002
Posts: 4
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)



}
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
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.,


Spritle Software Blogs
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 230

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.


Debashish
SCJP2, SCWCD 1.4, PMP, ITIL Foundation
 
wood burning stoves
 
subject: servlet calling an applet