• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with Tomcat

 
huaern Lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java code which is running perfectly well on Jbuilder. but after I install Tomcat and move my java file into tomcat folder, there are always error messages come out.

Exception: com.mysql.jdbc.Driver
java.lang.NullPointerException
at sei.Database.match(ControlClass.java:479)
at sei.Analyzer.checkAdd(ControlClass.java:317)
at sei.ControlClass.main(ControlClass.java:77)
Exception in thread "main" java.lang.NullPointerException
at sei.Database.shutdown(ControlClass.java:670)
at sei.Analyzer.checkAdd(ControlClass.java:318)
at sei.ControlClass.main(ControlClass.java:77)

C:\projects\Tomcat\server\webapps\SEI\WEB-INF\classes>
 
huaern Lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my java code
//package AutoPickUp;
package sei;

/**DD
* <p>Title: Auto Pick Up System</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

import java.io.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
import java.text.*;

public class ControlClass {
// DBTableModel dBTableModel1 = new DBTableModel();

public boolean validMessage(String message){
//Validate your messaeg
//return true if it isi ok
return false;
}
public String processMessage(String message){
//write ur code here
//make sure u return some String message;
return "not yet process";
}
public static void main (String[]args) throws IOException{
String Location, SMS,dateq;
String choice_string;
int choice, callid;

choice = 1;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

while (choice != 0){
System.out.println("******************************************************");
System.out.println(" Auto Logestic Pickup System ");
System.out.println("------------------------------------------------------");
System.out.println(" 1 Enter New Request ");
System.out.println(" 2 Check Job Schedule");
System.out.println(" 3 Check Resource List");
System.out.println(" 4 Change Job Schedule");
System.out.println(" 5 Change Resource List");
System.out.println(" 0 Exit");
System.out.println("------------------------------------------------------");

boolean test = false;
while (!test){
System.out.println("Please make a selection from 0 to 5: ");
choice_string = stdin.readLine();
if(choice_string.equals("1") || choice_string.equals("2") || choice_string.equals("3") || choice_string.equals("4") || choice_string.equals("5") ||choice_string.equals("0")){
test = true;
choice = Integer.parseInt(choice_string);
}
else
;
}
switch(choice){
// CASE 1
case 1:
String select = "Y";
while(select.equals("Y")|| select.equals("y")){
System.out.println("...................................................................");
System.out.println(". Please Enter your Request In Such A Following Format .");
System.out.println("...................................................................");
System.out.println(". Pick-up address / date request(ddmmyyy) / contact number . ");
System.out.println(". don't forget to put / between two information .");
System.out.println("...................................................................");
SMS = stdin.readLine();
Analyzer analyzer = new Analyzer (SMS);
String ad = analyzer.getAddress();
if (!ad.equals("")){
String adc = analyzer.checkAdd(ad); //adc the the address retrieve from database
if (adc == "NA") {
System.out.println("ERROR: Can not find your address '" + ad + "'");
}
else {
System.out.println("address validation -------- passed!");
if (analyzer.checkDate()) {
System.out.println("date validation -------- passed!");
analyzer.setAddress(adc);
dateq = analyzer.getDate();
callid = Integer.parseInt(analyzer.getCallid());
Schedule sdu = new Schedule();
sdu.addJob(adc, dateq, callid);
/* System.out.println("Address :" + analyzer.getAddress());
System.out.println("Request Date :" + analyzer.getDate());
System.out.println("Contact No :" + analyzer.getCallid());
*/
}
}
}

System.out.println("Do you want to begin another request? [Y/N]");
select = stdin.readLine();
if (select.equals("Y")|| select.equals("y") || select.equals("N") || select.equals("n")){
;
}
else {
System.out.println("Please key in Y or N to proceed");
}
}
break;
//CASE 2
case 2:
int choice2 = 1;
while(choice2 != 0){
System.out.println("...................................................................");
System.out.println(". Please select your destined way .");
System.out.println("...................................................................");
System.out.println(". 1 search data by date .");
System.out.println(". 2 show whole in data table .");
System.out.println(". 0 back to previous search .");
System.out.println("...................................................................");

boolean test2 = false;
String choice_string2 = "";
while (!test2){
System.out.println("Please make a selection from 0 to 2: ");
choice_string2 = stdin.readLine();
if(choice_string2.equals("1") || choice_string2.equals("2")||choice_string2.equals("0")){
test2 = true;
choice2 = Integer.parseInt(choice_string2);
}
else
;
}

Database db2 = new Database();
String dqq = "";
switch (choice2){
case 1:

int check2 = 1;
while (check2 != 0){
System.out.println("please enter date in 'DDMMYYYY' form");
dqq = stdin.readLine();
int datec = Integer.parseInt(dqq);
DateValidation dv = new DateValidation(datec);
if(dv.dateValidate(datec)){
db2.getConnect();
db2.show(dqq);
db2.shutdown();
check2 = 0;
}
}
break;
case 2:
dqq = "";
db2.getConnect();
db2.show(dqq);
db2.shutdown();
break;
case 0:
choice2 = 0;
break;
}
}
break;
//CASE 3
case 3:
int choice3 = 1;
while(choice3 != 0){
System.out.println("...................................................................");
System.out.println(". Please select your destined way .");
System.out.println("...................................................................");
System.out.println(". 1 search by bus number .");
System.out.println(". 2 show whole in data table .");
System.out.println(". 0 back to previous search .");
System.out.println("...................................................................");

boolean test3 = false;
String choice_string3 = "";
while (!test3){
System.out.println("Please make a selection from 0 to 2: ");
choice_string3 = stdin.readLine();
if(choice_string3.equals("1") || choice_string3.equals("2")||choice_string3.equals("0")){
test3 = true;
choice3 = Integer.parseInt(choice_string3);
}
else
;
}

Database db3 = new Database();
String dqq = "";
switch (choice3){
case 1:

int check3 = 1;
while (check3 != 0){
System.out.println("please key in full bus number e.g. 'SQL9867'");
dqq = stdin.readLine();
dqq.toUpperCase();
db3.getConnect();
db3.showResource(dqq);
db3.shutdown();
check3 = 0;
}
break;
case 2:
dqq = "";
db3.getConnect();
db3.showResource(dqq);
db3.shutdown();
break;
case 0:
choice3 = 0;
break;
}
}
break;
//CASE 4
case 4:
String select4 = "Y";
while(select4.equals("Y")|| select4.equals("y")){
System.out.println("...................................................................");
System.out.println(". Please Enter your destined Job ID e.g. 200411010001 .");
System.out.println("...................................................................");

String jobID = stdin.readLine();
Database db4 = new Database();
db4.getConnect();
db4.searchID(jobID);
db4.shutdown();
System.out.println("Is this the record that you are looking for? [Y/N]");
String selectc4 = stdin.readLine();
boolean test4 = false;
while (!test4){
if (selectc4.equals("Y")|| selectc4.equals("y") || selectc4.equals("N") || selectc4.equals("n")){
test4 = true;
}
else {
System.out.println("Please key in Y or N to proceed");
}
}

while(selectc4.equals("Y") || selectc4.equals("y")){

System.out.println("...................................................................");
System.out.println(". Please Enter your Request In Such A Following Format .");
System.out.println("...................................................................");
System.out.println(". Pick-up address / date request(ddmmyyy) / contact number . ");
System.out.println("...................................................................");

SMS = stdin.readLine();
Analyzer analyzer4 = new Analyzer (SMS);
String ad4 = analyzer4.getAddress();
if (!ad4.equals("")){
String adc4 = analyzer4.checkAdd(ad4); //adc4 the the address retrieve from database
if (adc4 == "NA") {
System.out.println("Can not find your address '" + ad4 + "'");
}
else {
if (analyzer4.checkDate()) {
analyzer4.setAddress(adc4);
dateq = analyzer4.getDate();
callid = Integer.parseInt(analyzer4.getCallid());
Schedule sdu4 = new Schedule();
sdu4.updateJob(jobID,adc4, dateq, callid);
selectc4 = "N";
}
}
}
}
System.out.println("Do you want to update another record? [Y/N]");
String answer = stdin.readLine();
if(answer.equals("N")|| answer.equals("n")){
select4 = "N";
}
}
break;
//CASE 5
case 5:
break;
//case 0
case 0:
choice = 0;
System.out.println("Bye!");
break;
}

}

}

}

/*================================================
Analyzer Class
==============================*/
class Analyzer {

private String address, dateq, callid;

public Analyzer (String s) {
address = "";
dateq= "";
callid ="";
StringTokenizer st = new StringTokenizer(s, "/",false);
if (st.countTokens() !=3){
System.out.println("Please Enter Request with correct form!");
}
else
this.address = st.nextToken();
this.dateq=st.nextToken();
this.callid= st.nextToken();
}
public String checkAdd(String a){

Database db = new Database();
db.getConnect();
String ad = db.match(a);
db.shutdown();

return ad;
}

public boolean checkDate(){
int date;
DateValidation newDateValidation;

date = Integer.parseInt(this.dateq);
newDateValidation = new DateValidation(date);

if (newDateValidation.monthNumberWithinRange()) {
if (newDateValidation.dayNumberWithinRange())
return true;
else
return false;
}
else
return false;

}

public String getAddress(){
return this.address;
}

public String getDate(){
return this.dateq;
}

public String getCallid(){
return this.callid;

}

public void setAddress(String adr){
this.address = adr;
}

public void setDate(String date){
this.dateq = date;
}

public void setCallid(String cid){
this.callid = cid;

}

}

/*========================================================
class client......
=======================================*/

class Client {

private String address, dateq, callid, jobid;


}

/*==========================================
Class Schedule
================================*/
class Schedule{

//add new job to schedule
public void addJob(String ad ,String dateq, int callid){
int cday, cmonth,cyear,cdate,sn;
String ndate,sn_string, njobid,pdate;

cdate = Integer.parseInt(dateq);
cday = cdate/1000000;
cmonth = (cdate%1000000)/10000;
cyear = cdate%10000;
if(cday <10){
ndate = "" + cyear + "" + cmonth + "0" + cday;
dateq = "0"+cday +""+cmonth+""+cyear;
}
else{
if (cmonth < 10) {
ndate = "" + cyear + "0" + cmonth + "" + cday;
dateq = "" + cday + "0" + cmonth + "" + cyear;
}
else
ndate = "" + cyear + "" + cmonth + "" + cday;
}

Database db1 = new Database();
db1.getConnect();
String jobid = db1.getJobID();
pdate = jobid.substring(0,8);

if(pdate.equals(ndate)){
//same day
sn_string = jobid.substring(8);
sn = Integer.parseInt(sn_string);
sn = sn + 1;
if (sn <10){
sn_string = "000" + sn;
}
else
if(sn<100){
sn_string = "00" + sn;
}
else
if(sn<1000){
sn_string= "0"+sn;
}
njobid = ndate+sn_string;
db1.insert(njobid, ad, dateq, callid, 0, "");
db1.shutdown();
}
else{
njobid = ndate + "0001";
db1.insert(njobid, ad, dateq, callid, 0, "");
db1.shutdown();
}
}
//update the job schedule
public void updateJob(String jobid, String address, String dateq, int callid){
Database dbu = new Database();
dbu.getConnect();
dbu.update(jobid, address, dateq, callid,0,"");
dbu.shutdown();

}
//delete undesired
public void deleteJob(String address, String dateq, String callid, String jobid, int status){

}

}

/*============================================
Class Database
=============================*/
class Database{
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
String name,query,jobid;

public void getConnect(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///db","root","211231");
if (!con.isClosed()) {
;
}
}
catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}

public String match(String s){ //used for matching purpose
query = "SELECT * FROM address where match (address) against "+"('"+s+"')";
try{

stmt = con.createStatement();
rs = stmt.executeQuery(query);
rs = stmt.getResultSet();

if (rs.next()==true){
name = rs.getString(2);
}
else {
//System.out.println("are you sure your address is : " + s +"?");
name = "NA";
}

rs.close();

} catch(Exception e) {
e.printStackTrace();
}

return name;
}

//******************used for insertion only of table schedule
public void insert(String a,String b,String c,int d,int sd, String f){
//here can add in more variables for laler use , jobid,,,etc
query = "insert into schedule (jobid,address,dateq,callid,status,detail) values "+"('"+a+"','"+b+"','"+c+"',"+d+","+sd+",'"+f+ "');";
try{

stmt = con.createStatement();
int i = stmt.executeUpdate(query);

if(i==1){
System.out.println("Request has been successfully added!");

query = "select * from schedule where callid = "+d+" and address = '"+b+"';";
rs = stmt.executeQuery(query);
rs = stmt.getResultSet();

while (rs.next()) {
ResultSetMetaData md = rs.getMetaData();
for (int co = 1; co <= md.getColumnCount(); co++) {
System.out.print(md.getColumnName(co)+" : "+rs.getString(co) + "\n");
}
System.out.println("");
}
}
} catch(Exception e) {
e.printStackTrace();
}


}

public void insert (String ss){ //used for address table insertion
query = "insert into address (address) values "+"('"+ss+"');";
try{

stmt = con.createStatement();
rs = stmt.executeQuery(query);

} catch(Exception e) {
e.printStackTrace();
}
System.out.println("insert done!");

}

public void update(String a,String b,String c,int d,int sd, String f){ //used for updation only

query = "update schedule set address='"+b+"',dateq='"+c+"',callid="+d+",status="+sd+",detail='"+f+"' where jobid = '"+a+"';";
try{

stmt = con.createStatement();
int i = stmt.executeUpdate(query);

if(i==1){
System.out.println("Updating has been successfully done!");

query = "select * from schedule where jobid = '"+a+"';";
rs = stmt.executeQuery(query);
rs = stmt.getResultSet();

while (rs.next()) {
ResultSetMetaData md = rs.getMetaData();
for (int co = 1; co <= md.getColumnCount(); co++) {
System.out.print(md.getColumnName(co)+" : "+rs.getString(co) + "\n");
}
System.out.println("");
}
}
} catch(Exception e) {
e.printStackTrace();
}

}

public void delete(){ //used for deletetion only

}

public String getJobID(){
query = "select jobid from schedule order by jobid desc limit 1;";
try{

stmt = con.createStatement();
rs = stmt.executeQuery(query);

while (rs.next()) {
jobid = rs.getString(1);
}

} catch(Exception e) {
e.printStackTrace();
}

return jobid;
}

public void show(String dq){
if (dq == "")
query = "select * from schedule;";
else
query = "select * from schedule where dateq = '"+dq+"';";

try {
stmt = con.createStatement();
rs = stmt.executeQuery(query);
rs = stmt.getResultSet();

System.out.println("id | jobid | address |date request| callid |status| details|");
while (rs.next()) {
ResultSetMetaData md2 = rs.getMetaData();
for (int col = 1; col <= md2.getColumnCount(); col++) {
System.out.print(rs.getString(col)+" | ");
}
System.out.println("");
}

}catch(Exception e) {
e.printStackTrace();
}
}

public void showResource(String r){
if (r == "")
query = "select * from resource;";
else
query = "select * from resource where number = '"+r+"';";

try {
stmt = con.createStatement();
rs = stmt.executeQuery(query);
rs = stmt.getResultSet();

System.out.println("id|number|jobid|details|");
while (rs.next()) {
ResultSetMetaData md2 = rs.getMetaData();
for (int col = 1; col <= md2.getColumnCount(); col++) {
System.out.print(rs.getString(col)+" | ");
}
System.out.println("");
}

}catch(Exception e) {
e.printStackTrace();
}
}

public void searchID(String jid){
query = "select * from schedule where jobid = '"+jid+"';";

try{
stmt = con.createStatement();
rs = stmt.executeQuery(query);
rs = stmt.getResultSet();

while (rs.next()) {
ResultSetMetaData md = rs.getMetaData();
for (int co = 1; co <= md.getColumnCount(); co++) {
System.out.print(md.getColumnName(co)+" : "+rs.getString(co) + "\n");
}
System.out.println("");
}
} catch(Exception e) {
e.printStackTrace();
}
}

public void shutdown(){ //used for disconnection
try {
if(con != null)
stmt.close();
con.close();
} catch(SQLException e) {}
}

}

/*==================================
class Trucklist
=============================*/

class Trucklist{
private String date;

//addin here to check whether some trucks are free???
public boolean checkTruck(String a, String b){
return true;
}


}
/*==============================
class Truck
================*/

class Truck{
private String ID, date;
private int status;

//constructor
Truck(String a, String b, int c){
this.ID = a;
this.date = b;
this.status = c;
}
}

/*====================================
date validation class
===================================*/
class DateValidation {

// ------------------ FIELDS ------------------------
static private final int MIN_MONTH_NUMBER = 1;
static private final int MAX_MONTH_NUMBER = 12;
static private final int MIN_DAY_NUMBER = 1;
private int dayNumber;
private int monthNumber;
private int yearNumber;

// ------------------ CONSTRUCTORS ------------------------

/* ------ DATE VALIDATION ------ */

public DateValidation(int dateCode) {
dayNumber = dateCode/1000000;
monthNumber = (dateCode%1000000)/10000;
yearNumber = dateCode%10000;
}

public boolean monthNumberWithinRange() {

if ((monthNumber < MIN_MONTH_NUMBER) || (monthNumber > MAX_MONTH_NUMBER)) {
System.out.println("ERROR: Month Number [" + monthNumber + "] is an invalid month number");
return(false);
}
else
return(true);
}

public boolean dayNumberWithinRange() {
if ((dayNumber < MIN_DAY_NUMBER) || (dayNumber > getNumDaysInMonth())) {
System.out.println("ERROR: Year Number [" + dayNumber + "] is an invalid day number");
return(false);
}
else
return(true);
}

/* ------ NUMBER OF DAYS IN MONTH ------ */

private int getNumDaysInMonth() {
int numDaysInMonth=0;

switch(monthNumber) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
numDaysInMonth = 31;
break;
case 4: case 6: case 9: case 11:
numDaysInMonth = 30;
break;
case 2:
// Test for leap year
if (((yearNumber%4 == 0) && (yearNumber%100 != 0)) || (yearNumber%400 == 0))
numDaysInMonth = 29;
else
numDaysInMonth = 28;
break;
default:
System.out.println("ERROR: Cause unknown");
}
return(numDaysInMonth);

}

public boolean dateValidate(int ddc){
DateValidation newdv;

newdv = new DateValidation(ddc);

if (newdv.monthNumberWithinRange()) {
if (newdv.dayNumberWithinRange())
return true;
else
return false;
}
else
return false;
}
}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the mysql jdbc JAR in your WEB-INF/lib directory?

The exception looks like the code to load the driver in your Database class:

is failing and a Connection is never being created. Since no exception is thrown from this method, the program continues along, trying to use the null Connection and throwing NullPointerException every time it does so.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!