null pointer exception --- urgent

Reply

Join Date: Jan 2008
Posts: 26
Reputation: abdulraqeeb33 is an unknown quantity at this point 
Solved Threads: 0
abdulraqeeb33 abdulraqeeb33 is offline Offline
Light Poster

null pointer exception --- urgent

 
0
  #1
Apr 23rd, 2008
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package server;

/**
*
* @author 007
*/
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Random.*;
import java.net.*;
import java.lang.String.*;
import java.io.InputStream;
import java.lang.Object.*;
import java.net.ServerSocket.*;



public class aptserver implements Runnable{

private static String password;

private Socket aptsocket=null;
int c=0;
static ResultSet res=null;
static Connection conn=null;
static Statement stat=null;
private static String servoff=null;
private static int servoff_port=0;
//private static int laserport;
//private static int adaptport;

public static void main(String[] ar) throws Exception
{
try
{
int port=14007; //the only constant port

ServerSocket aptsrv= new ServerSocket(port);
int count=0;

while(true)
{
Socket aptsocket=aptsrv.accept();
Runnable runnable=new aptserver(aptsocket,++count);
Thread thread=new Thread(runnable);
thread.start();
}
}//try
catch(Exception e) {
System.out.println("The error is in the main:"+e.getMessage());

}
}//main

aptserver(Socket s,int c)
{
this.aptsocket=s;
this.c=c;
}


public void run()
{

try
{
System.out.println("Got a client (client num : "+c+")");
System.out.println();
InputStream sin = aptsocket.getInputStream();
OutputStream sout = aptsocket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String username="";
String passrec="";
String ip="";
String mac="";


InetAddress myself = InetAddress.getLocalHost ();
String serverip=myself.toString();
serverip=serverip.substring(4);

int flag=0; int port_array[]= new int[10];
while(true)
{
username = in.readUTF();
System.out.println("recving username:"+username);
passrec=in.readUTF();
System.out.println("srecvng password:"+passrec);
ip=in.readUTF();
System.out.println("recving ip:"+ip);
mac=in.readUTF();
System.out.println("recvieg mac:"+mac);
port_array=getports();
flag= auth(username,passrec,ip,mac,port_array[1]);
if(flag==1)
{

out.writeUTF("Success");
System.out.println("laserport got from db:"+port_array[0]);
System.out.println("adport got from db:"+port_array[1]);
System.out.println("serverip got from db:"+serverip);
System.out.println("servoff got from db:"+servoff);
System.out.println("servoff_port got from db:"+servoff_port);
out.flush();
out.writeInt(port_array[0]);
out.flush();
out.writeInt(port_array[1]);
out.flush();
out.writeUTF(serverip);
out.flush();
out.writeUTF(servoff);
out.flush();
out.writeInt(servoff_port);
out.flush();

String[] serv=null;


serv[0]=String.valueOf(port_array[0]);//seending the port # of client who has the server socket
System.out.println("Entering into db");
enterintodb(port_array[1],servoff,servoff_port,ip,mac);
//serv[1]=servoff;
//serv[2]=String.valueOf(servoff_port);
server.lookupserver.main(serv);


}
else
{
out.writeUTF("Mismatch");
out.flush();
}

break;

//Thread.sleep(4000);
}//while
}//try

catch(Exception x)
{
System.out.println("the error is "+x);
}


}

private static int auth(String usrn, String pwd, String ipaddr, String macaddr, int adaptport) throws ClassNotFoundException, SQLException
{
//usrn="raf123";ipaddr="169.254.175.83";macaddr="00-0F-B8-UJ-9K-D3";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//DONOT CHANGE THIS
conn = DriverManager.getConnection("jdbc:odbc:MS Access Database");//msk is the DSN Name
stat = conn.createStatement();

res = stat.executeQuery("Select service,service_port from valid_clients where ip='"+ipaddr+"' and mac='"+macaddr+"'"); // Customer is the table name
res.next(); // initially points to first data
try{
//System.out.println("valid user"); //displays first attribute
servoff=res.getString(1); //displays second attribute
servoff_port=res.getInt(2);
}

catch(SQLException e){
System.out.println("Intruder alert in here");
stat.execute("insert into blocked values('"+ipaddr+"','"+macaddr+"')");
//intruder alert..work her
}


char a,c,b,d,e,f;
String g;

a=uname(usrn);
b=ipnum(ipaddr);
c=ipalpha(ipaddr);
d=macnum(macaddr,6,7,1);
e=macnum(macaddr,9,10,2);
f=macnum(macaddr,15,16,2);
g=fromdb(usrn,ipaddr);
password = String.valueOf(a)+String.valueOf(b) +String.valueOf(c)+String.valueOf(d)+String.valueOf(e)+String.valueOf(f)+String.valueOf(g);
//System.out.println("the calc password is:"+password);
if(password!=null && pwd != null)
{
if(password.equals(pwd))

{
try{
System.out.println("inserting the values in database");
stat.execute("insert into clients_online values('" + ipaddr + "','" + macaddr + "','" + servoff_port + "','1','"+servoff + "','"+adaptport+"','"+usrn+"')");
//stat.execute("insert into blocked values('168.ad','aa-xx')");

}
catch (SQLException ex) {
System.out.println("Unable to connect to the database in auth"+ex.getMessage());
}
return 1;
}
else
return 0;
}
return 0;
}

private static String fromdb(String name, String ip)
{
ResultSet resdb;
Connection conndb;
Statement statdb;
int j=0;char a,b;String keys="";
for(int i=0;i<ip.length();i++)
{
b=ip.charAt(i);
if(b=='.')
{
continue;
}
j+=b-'0';
//System.out.println(j);
}
if(j>10)
{
j=j%10;
}

try
{
int flag=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conndb = DriverManager.getConnection("jdbc:odbc:MS Access Database");
statdb = conndb.createStatement();
resdb = statdb.executeQuery("SELECT * FROM uname ");
while(resdb.next())
{

if(resdb.getString(1).equals(name))
{
keys=resdb.getString(2);
flag=1;
break;
}
}//while
if(flag==0)
{
System.out.println("No rows found");
}
}//try

catch(Exception e)
{
System.out.println("Error in 1st(no username in the table) " +e);
}

a=keys.charAt(0);
b=keys.charAt(1);

int c=a-'A'+1;
int d=b-'A'+1;

c+=j;d+=j;

while(c>26)
{
c=c%26;
}
while(d>26)
{
d=d%26;
}
c+=64;
d+=64;
a=(char) c;
b=(char) d;

keys=String.valueOf(a)+String.valueOf(b);
return (keys);
}

private static char ipalpha(String ip)
{
int j=0,count=0;char b,d;
for(int i=ip.length()-1;i>0;i--)
{
b=ip.charAt(i);
if(count==2)
{
break;
}
if(b=='.')
{
count++;
continue;
}
j+=b-'0'; //to get the number out of the number itself
//System.out.println(j);
}

j=j%10;
j=j+48; //getting back the number
d=(char) j;
return d;
}

private static char ipnum(String ip)
{
int j=0,count=0;char b;
for(int i=0;i<ip.length();i++)
{
b=ip.charAt(i);
if(count==2)
{
break;
}
if(b=='.')
{
count++;
continue;
}
j+=b-'0';
//System.out.println(j);
}//for
while(j>10)
{
j=j%10;
}
b=(char) (j+48);
return (b);
}

private static char macnum(String mac, int first, int sec, int flag)
{
char temp;int j=0;char d;
if(Character.isDigit(temp=mac.charAt(first)))
{
j+=temp-'0';
// b=(char) (j+64);

}
else
{
j+=temp-'A'+1;
}
if(Character.isDigit(temp=mac.charAt(sec)))
{
j+=temp-'0';
}
else
{
j+=temp-'A'+1;
}
if(flag==1)
{
while(j>26)
{
j=j%26;
}
d=(char) (j+64);
return d;
}
else
{
j=j%10;
j=j+48; //getting back the number
d=(char) j;
return d;
}
}

private static char uname(String unam)
{
int j=0;char b;
for(int i=unam.length()-1;i>2;i--)
{
b=unam.charAt(i);
j+=b-'0';
}
b=(char) (j+64);
//System.out.println("Uname last three characters sum upto:"+j);
//System.out.println(b);
return (b);
}

private void enterintodb(int adport, String servoff, int servoff_port,String ip,String mac) {

Connection connlu;
Statement statlu;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connlu = DriverManager.getConnection("jdbc:odbc:MS Access Database");
statlu = connlu.createStatement();

statlu.execute("update clients_online set servoff = '"+servoff+"' where ip='"+ip+"' and mac='"+mac+"'");
statlu.execute("update clients_online set servport = '"+servoff_port+"' where ip='"+ip+"' and mac='"+mac+"'");
statlu.execute("update clients_online set adaptport = '"+adport+"' where ip='"+ip+"' and mac='"+mac+"'");

}
catch(Exception x)
{
System.out.println("unable to connect to the database in enterintodb");
System.out.println(x);
}


}


private int[] getports()
{
ResultSet resad;
Connection connad;
Statement statad;

int array[] = new int[10];


try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connad = DriverManager.getConnection("jdbc:odbc:MS Access Database");
statad = connad.createStatement();
resad = statad.executeQuery("SELECT * FROM port where index="+c+"");
resad.next();
array[1]=resad.getInt(3);//adapt port
array[0]=resad.getInt(2);//lookup port
System.out.println("adaptport from db:"+array[1]);
System.out.println("laserport from db:"+array[0]);


}
catch(Exception x)
{
System.out.println(x);
System.out.println("Unable to connect to database in laport");
}
return array;

}





}


the entire thing sends the info to the client, but at last just when the lookupserver() pgm has to execute...it catches a null pointer exception error and stops...the program control doesnt goto to the lookupserver();

its multi thread program which works on a socket level...
one port...but different socket each time a new client enters
plz help me...the final proj submission date is soon approaching
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: null pointer exception --- urgent

 
0
  #2
Apr 23rd, 2008
why don't you post the exception and stack trace, and post the code inside code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 26
Reputation: abdulraqeeb33 is an unknown quantity at this point 
Solved Threads: 0
abdulraqeeb33 abdulraqeeb33 is offline Offline
Light Poster

Re: null pointer exception --- urgent

 
0
  #3
Apr 23rd, 2008
Originally Posted by bugmenot View Post
why don't you post the exception and stack trace, and post the code inside code tags
thanks for replying....
init:
deps-jar:
compile-single:
run-single:
Got a client (client num : 1)

recving username:raf123
srecvng password:F75L14BX
recving ip:169.254.86.65
recvieg mac:00-0F-66-74-4F-C1
adaptport from db:49581
laserport from db:49158
inserting the values in database
Unable to connect to the database in authGeneral error
laserport got from db:49158
adport got from db:49581
serverip got from db:169.254.244.149
servoff got from db:FILE
servoff_port got from db:8000
the error is java.lang.NullPointerException

this is the output..
wat is this stacktrace...
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,327
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 248
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: null pointer exception --- urgent

 
0
  #4
Apr 23rd, 2008
  1. server.lookupserver.main(serv);

Where is any of the above (aside from the String array serv) declared and/or defined.

According to your code, server is a package, so the only way this could possibly work, is if "lookupserver" were a class within server, and main a static method in that class.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 26
Reputation: abdulraqeeb33 is an unknown quantity at this point 
Solved Threads: 0
abdulraqeeb33 abdulraqeeb33 is offline Offline
Light Poster

Re: null pointer exception --- urgent

 
0
  #5
Apr 23rd, 2008
the exception is caught just after run()



Originally Posted by abdulraqeeb33 View Post
thanks for replying....
init:
deps-jar:
compile-single:
run-single:
Got a client (client num : 1)

recving username:raf123
srecvng password:F75L14BX
recving ip:169.254.86.65
recvieg mac:00-0F-66-74-4F-C1
adaptport from db:49581
laserport from db:49158
inserting the values in database
Unable to connect to the database in authGeneral error
laserport got from db:49158
adport got from db:49581
serverip got from db:169.254.244.149
servoff got from db:FILE
servoff_port got from db:8000
the error is java.lang.NullPointerException

this is the output..
wat is this stacktrace...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 26
Reputation: abdulraqeeb33 is an unknown quantity at this point 
Solved Threads: 0
abdulraqeeb33 abdulraqeeb33 is offline Offline
Light Poster

Re: null pointer exception --- urgent

 
0
  #6
Apr 23rd, 2008
yeah there is a class in server package namely lookupserver


Originally Posted by masijade View Post
  1. server.lookupserver.main(serv);

Where is any of the above (aside from the String array serv) declared and/or defined.

According to your code, server is a package, so the only way this could possibly work, is if "lookupserver" were a class within server, and main a static method in that class.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 26
Reputation: abdulraqeeb33 is an unknown quantity at this point 
Solved Threads: 0
abdulraqeeb33 abdulraqeeb33 is offline Offline
Light Poster

Re: null pointer exception --- urgent

 
0
  #7
Apr 23rd, 2008
please check the run block...its there that the error occurs

Originally Posted by abdulraqeeb33 View Post
yeah there is a class in server package namely lookupserver
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,327
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 248
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: null pointer exception --- urgent

 
0
  #8
Apr 23rd, 2008
The basis of your error, however, seems to be that you cannot connect to the DB. Are you sure that that the dsn is defined as "MS Access Database" as you have in your code.

If msk is the DSN (as your comment suggests), then use that.
Last edited by masijade; Apr 23rd, 2008 at 7:21 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 26
Reputation: abdulraqeeb33 is an unknown quantity at this point 
Solved Threads: 0
abdulraqeeb33 abdulraqeeb33 is offline Offline
Light Poster

Re: null pointer exception --- urgent

 
0
  #9
Apr 23rd, 2008
no no...the database stuff is perfectly fine else it wudnt insert or retrieve tuples from the db...
u can check the output i posted a little earlier..
plz see the run() block...its there that the error is occuring


Originally Posted by masijade View Post
The basis of your error, however, seems to be that you cannot connect to the DB. Are you sure that that the dsn is defined as "MS Access Database" as you have in your code.

If msk is the DSN (as your comment suggests), then use that.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,327
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 248
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: null pointer exception --- urgent

 
0
  #10
Apr 23rd, 2008
Then what is this
"Unable to connect to the database in authGeneral error"
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC