Getting a NULL pointer exception on a input stream - java

Not quite sure why but I am getting a null pointer exception when declaring what input stream is and what it should take in (converting string in an input stream), the get.bytes (converting from string to input reader) I read up online and implemented
public void processTables()throws SQLException
{
dmd = con.getMetaData();
ResultSet schema= dmd.getSchemas();
ResultSet result = dmd.getTables(null,null,"%",null);
while(result.next()) {
String tablename = result.getString(3);
ResultSet column = dmd.getColumns(null,null,tablename,null);
ResultSetMetaData rmd = result.getMetaData();
int count = rmd.getColumnCount();
while(column.next()) {
for(int i=1; i<=count; i++) {
String col=column.getString(i);
System.out.println("Column: " + col);
String question = null;
InputStream col2 = new ByteArrayInputStream(col.getBytes());
BufferedReader brin;
brin = new BufferedReader(new InputStreamReader(col2));
try {
question = brin.readLine();
FileWriter output = new FileWriter("Columns.txt");
BufferedWriter out = new BufferedWriter(output);
out.write(question);
out.close();
} catch (IOException io) {
System.out.println("hello");
}
}
ResultSet fk = dmd.getImportedKeys(con.getCatalog(),null,tablename);
while (fk.next()) {
String fkTableName = fk.getString("FKTABLE_NAME");
String fkColName = fk.getString("FKCOLUMN_NAME");
String pkTableName = fk.getString("PKTABLE_NAME");
String pkColName = fk.getString("PKCOLUMN_NAME");
System.out.println("Name of Table: " + tablename);
System.out.println("Foreign Key: [" + fkTableName + "." + fkColName + "] REFERENCES [" + pkTableName + "." + pkColName +"]\n");
}
}
}
JAVA STACK TRACE down below
C:\Users\harma_000\Documents\Computer Science Year 2\SCC201\SCC201 13-14 CW Stud
ent Pack>java -classpath .;sqlite-jdbc4-3.8.2-SNAPSHOT.jar RefInteg
Db.constructor [lsh]
Db.Open : leaving
Column: null
Exception in thread "main" java.lang.NullPointerException
at Db.processTables(Db.java:42)
at RefInteg.checkDatabase(RefInteg.java:14)
at RefInteg.go(RefInteg.java:20)
at RefInteg.main(RefInteg.java:28)
C:\Users\harma_000\Documents\Computer Science Year 2\SCC201\SCC201 13-14 CW Stud
ent Pack>pause
Press any key to continue . . .

System.out.println("Column: " + col);
String question = null;
InputStream col2 = new ByteArrayInputStream(col.getBytes());
col is null, which is why col.getBytes() throws the NullPointerException
Btw. is is also indicated by your output Column: null

Related

Program with lots of queries just stops, no exceptions

I'm running a program that is making a query for several thousand individuals. About 2/3 of the way through the list, it just stops...no exception, nothing. It just won't continue.
I'm not sure exactly what is going on here, why it just stops. I don't see anything wrong with the data (which would generate an exception anyway). Am I doing too many queries in a row?
Thanks in advance for any suggestions.
File inputFile = new File(datafile);
BufferedReader br = new BufferedReader(new FileReader(inputFile));
List <WRLine> empList = new ArrayList<>();
String s;
int counter = 0;
while ((s = br.readLine()) != null) {
String[] sLine = s.split(",");
if (sLine.length > 3) {
try {
//if it's a number, it's not a name. Skip the line.
int i = Integer.parseInt(sLine[0].trim());
} catch (Exception e) {
//if it's not a number and not blank, add it to the list
if (!sLine[2].equals("")) {
try {
int q = Integer.parseInt(sLine[2].trim());
WRLine wr = new WRLine(sLine[0], sLine[2], sLine[3]);
empList.add(wr);
} catch (Exception ex) {
//continue
}
}
}
}
}
//empList contains 1,998 items
Map<String, Integer> resultMap = new HashMap<>();
Iterator i = empList.iterator();
try {
String connectionURL = "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + pw;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionURL);
PreparedStatement ps = null;
ResultSet rs = null;
String query = "";
while (i.hasNext()) {
WRLine wr = (WRLine) i.next();
System.out.println("Searching " + wr.getName() + "...");
query = "Select count(*) as APPLIED from request where (requestDate like '%2017%' or requestDate like '%2018%') AND officer=(select id from officer where employeenumber=?)";
ps = conn.prepareStatement(query);
ps.setString(1, wr.getEmployeeNum());
rs = ps.executeQuery();
while (rs.next()) {
int queryResult = rs.getInt("APPLIED");
//if the division is already in there
if (resultMap.containsKey(wr.getDivision())) {
Integer tmp = resultMap.get(wr.getDivision());
tmp = tmp + queryResult;
resultMap.put(wr.getDivision(), tmp);
} else {
resultMap.put(wr.getDivision(), queryResult);
}
}
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
//report by division
Summarizing what others have said in the comments, your problem could be due to improper JDBC resource handling. With Java 7 and above, you should use the try-with-resources statement, which frees resources automatically. Also, as of JDBC 4, you don't need to call Class.forName() explicitly. Finally, you should never prepare a PreparedStatement inside a loop when the only thing that changes is the bind variable.
Putting this together, the data access part could be rewritten as
String connectionURL = "jdbc:mysql://" + ip + ":" + port + "/" + dbName
+ "?user=" + userName + "&password=" + pw;
String query = "Select count(*) as APPLIED from request where "
+ "(requestDate like '%2017%' or requestDate like '%2018%') "
+ "AND officer=(select id from officer where employeenumber=?)";
try (Connection conn = DriverManager.getConnection(connectionURL);
PreparedStatement ps = conn.prepareStatement(query)) {
while (i.hasNext()) {
WRLine wr = (WRLine) i.next();
System.out.println("Searching " + wr.getName() + "...");
ps.setString(1, wr.getEmployeeNum());
// the result set is wrapped in its own try-with-resources
// so that it gets properly deallocated after reading
try (ResultSet rs = ps.executeQuery()) {
// SQL count is a scalar function so we can just use if instead of while
if (rs.next()) {
int queryResult = rs.getInt("APPLIED");
//if the division is already in there
if (resultMap.containsKey(wr.getDivision())) {
Integer tmp = resultMap.get(wr.getDivision());
tmp = tmp + queryResult;
resultMap.put(wr.getDivision(), tmp);
} else {
resultMap.put(wr.getDivision(), queryResult);
}
}
}
}
} catch (SQLException e) {
// consider wrapping as a RuntimeException and rethrowing instead of just logging
// because these are usually caused by
// programming errors or fatal problems with the DB
e.printStackTrace();
}

java return data to java client

i have java client send to server (jetty-xmlrpc) query and receive data from server inside hashmap. sometime data is more big(e.g. 3645888 rows), when this data send to java client i have error ( java heap space ). how can i send data by 2 times for example ? or give me way to fix it
this is server function to get data and send it to client
public HashMap getFlickValues(String query,String query2){
System.out.println("Query is : "+query);
System.out.println("Query2 is: "+query2);
Connection c = null;
Connection c2 = null;
Statement st = null;
Statement st2 = null;
HashMap<String, Object[]> result = new HashMap<String, Object[]>();
ArrayList<Double> vaArrL = new ArrayList<Double>();
ArrayList<Double> vbArrL = new ArrayList<Double>();
ArrayList<Double> vcArrL = new ArrayList<Double>();
try {
Class.forName("org.postgresql.Driver");
String conString = "jdbc:postgresql://" + host + ":" + port + "/" + DBName +
"?user=" + user + "&pass=" + pass;
String conString1 = "jdbc:postgresql://" + host + ":" + port2 + "/" + DBName2 +
"?user=" + user + "&pass=" + pass;
//String conString1 = "jdbc:postgresql://127.0.0.1:5431/merkezdbram " +
// "?user=" + user + "&pass=" + pass;
/*c = DriverManager.getConnection(conString);
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()){
vaArrL.add(rs.getDouble("va"));
vbArrL.add(rs.getDouble("vb"));
vcArrL.add(rs.getDouble("vc"));
}*/
c = DriverManager.getConnection(conString);
//c.setAutoCommit(false);
c2 = DriverManager.getConnection(conString1);
//c2.setAutoCommit(false);
st = c.createStatement();
//st.setFetchSize(1000);
st2 = c2.createStatement();
//st2.setFetchSize(1000);
List<ResultSet> resultSets = new ArrayList<>();
resultSets.add(st.executeQuery(query));
resultSets.add(st2.executeQuery(query2));
ResultSets rs = new ResultSets(resultSets);
int count = 0;
int ResultSetSize = rs.getFetchSize();
System.out.println("ResultSetSize is "+ResultSetSize);
while (rs.next()){
//count++;
//if ( count == 2200000) { break;}
vaArrL.add(rs.getDoubleVa("va"));
vbArrL.add(rs.getDoubleVb("vb"));
vcArrL.add(rs.getDoubleVc("vc"));
}
int sz = vaArrL.size();
result.put("va", vaArrL.toArray(new Object[sz]));
result.put("vb", vbArrL.toArray(new Object[sz]));
result.put("vc", vcArrL.toArray(new Object[sz]));
//rs.close();
st.close();
c.close();
} catch ( Exception e ) {
System.out.println(e);
e.printStackTrace();
}
System.out.println("Flicker vaArrL.size = "+vaArrL.size());
return result;
}
and ResultSets class is :
class ResultSets {
private java.util.List<java.sql.ResultSet> resultSets;
private java.sql.ResultSet current;
public ResultSets(java.util.List<java.sql.ResultSet> resultSets) {
this.resultSets = new java.util.ArrayList<>(resultSets);
current = resultSets.remove(0);
}
public boolean next() throws SQLException {
if (current.next()) {
return true;
}else if (!resultSets.isEmpty()) {
current = resultSets.remove(0);
return next();
}
return false;
}
public Double getDoubleVa(String va) throws SQLException{
return current.getDouble("va");
}
public Double getDoubleVb(String vb) throws SQLException{
return current.getDouble("vb");
}
public Double getDoubleVc(String vc) throws SQLException{
return current.getDouble("vc");
}
}
i want way to return data to client without (java heap space) ?
i make -Xmx1024m for VM argument , but same problrm
i want solution in my code
thanks

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException UNKNOWN COLUMN

I am currently trying to scan and parse the file that is not in sql format. I am trying to input all the data into the SQL table but for some reason every time i run the program, i get the error saying unknown column 'what' in 'field list.' So the neither of the data goes through. 'what' is one of the names that is on the text. The table currently has 11 columns. I know I am parsing or scanning it wrong but I cannot figure out where. Here is my code:
public class parseTable {
public parseTable (String name) throws FileNotFoundException
{
File file = new File(name);
parse(file);
}
private void parse(File file) throws FileNotFoundException
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionUrl = "jdbc:mysql://localhost:3306/";
String connectionUser = "";
String connectionPassword = "";
conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
stmt = conn.createStatement();
Scanner scan = new Scanner(file);
String[] rowInfo = new String[11];
int count = 0;
while(scan.hasNextLine()){
//String data = scan.nextLine();
Scanner lineScan = new Scanner(scan.nextLine());
while(lineScan.hasNext()){
String words = lineScan.next();
if(count < 11){
rowInfo[count] = words;
count++;
}
else if(count == 11 && words.equals("States")){
rowInfo[count - 1] = rowInfo[count - 1] + " " + words;
}
else{
String query = "";
for(int i = 0; i < rowInfo.length; i++)
{
if(query.equals(""))
{
query = rowInfo[i];
}
else if(i == 9){
query = query + "," + rowInfo[i];
}
else if(rowInfo[i].equals(null)){
query = query + ", " + "NULL";
}
else
query = query + ", " + "'" + rowInfo[i] + "'";
}
stmt.executeUpdate("INSERT INTO dup VALUES(" + query + ")");
count = 0;
rowInfo = new String[11];
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}
}
And this is the data I'm trying to input:
1 hello cheese 1111 what#yahoo.com user adm street zip what USA
2 Alex cheese 1111 what#yahoo.com user adm street zip what USA
So this is my new code now, using PrepareStatement. However I still get an error and I looked online for the solution on where I'm making a mistake, but I cant seem to figure out where.
String query = "INSERT INTO mil_table (UserName, NameFirst, NameLast, supportID, EmailAddress, Password,
IDQ, AddressCity, AddressState, AddressZip, AddressCountry) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(query);
Scanner scan = new Scanner(file);
String[] rowInfo = new String[11];
int count = 0;
while(scan.hasNextLine()){
//String data = scan.nextLine();
Scanner lineScan = new Scanner(scan.nextLine());
while(lineScan.hasNext()){
String words = lineScan.next();
if(count < 11){
rowInfo[count] = words;
count++;
}
else if(count == 11 && words.equals("States")){
rowInfo[count - 1] = rowInfo[count - 1] + " " + words;
}
else{
for(int i = 0; i <rowInfo.length; i++)
{
pstmt.setString(i + 1, rowInfo[i]);
}
//stmt.executeUpdate("INSERT INTO mil_table VALUES(" + query + ")");
//System.out.println("#" + query + "#");
pstmt.executeUpdate();
count = 0;
rowInfo = new String[11];
}
}
As you are using MySQL, you will need to enclose the text inputs with quotes. Try enclosing the String values that you are inserting in quotes and then execute your code.

How do I make the weird characters in Spanish go away? It persists even after changing JDB URL to UTF-8

I see words such as súbito, autónomo. Why aren't they proper. I had a problem while entering all Russian characters via JDBC into the MySQL database. The problem there was that the Russian characters were appearing as ???? instead of the words. That got fixed when I changed the JDBC URL to have UTF-8 encoding
jdbc:mysql://localhost/metaphor_repository?characterEncoding=utf8"
Doing the same does not fix the problem here.
public void readPatterns() throws FileNotFoundException, IOException, InstantiationException, ClassNotFoundException, IllegalAccessException, SQLException {
//Code to initialize database and stuff
PreparedStatement preparedStatement = null;
String key1 = null;
String databaseURL = "jdbc:mysql://localhost/metaphor_repository?characterEncoding=utf8";
String databaseUser = "root";
String databasePassword = "D0samrD9";
String dbName = "metaphor_repository";
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(databaseURL, databaseUser, databasePassword);
System.out.println("CONNECTED");
String insertTableSQL = "INSERT INTO source_domain_spanish_oy2_jul2014_2(filename, seed, words, frequency, type, after_before) VALUES(?,?,?,?,?,?);";
String foldername = "/Desktop/Espana/AdjectiveBefore/";
File Folder = new File(foldername);
File[] ListOfFiles = Folder.listFiles();
for (int x = 0; x < ListOfFiles.length; x++) {
File file = new File(ListOfFiles[x].getAbsolutePath());
InputStream in = new FileInputStream(file);
InputStreamReader reader1 = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader1);
String fileData = new String();
String filename = ListOfFiles[x].getName().toUpperCase();
int total;
BufferedWriter out;
FileWriter fstream;
BufferedWriter outLog;
String fileName = new String("/Desktop/Espana/AdjectiveBeforeResult/" + ListOfFiles[x].getName());
fstream = new FileWriter(fileName);
out = new BufferedWriter(fstream);
while ((fileData = br.readLine()) != null) {
Map<String, Integer> sortedMapDesc = searchDatabase(fileData);;
//Code Written By Aniruth to extract some info: seed, before_after
String seed = fileData;
String before_after = seed.split("\\[")[0];
seed = seed.replaceAll("\\(v.\\)", "");
seed = seed.replaceAll("\\(n.\\)", "");
seed = seed.substring(seed.indexOf("]") + 1, seed.indexOf("."));
seed = seed.substring(seed.indexOf("[") + 1, seed.indexOf("]"));
seed = seed.replaceAll("'", "");
seed = seed.trim();
seed = seed.toUpperCase();
Set<String> keySet = sortedMapDesc.keySet();
total = 0;
Iterator<String> keyItr = keySet.iterator();
out.write("++++++++++++++++++++++++++++++++++++++++++\n");
if (sortedMapDesc.isEmpty()) {
out.write(fileData + "\n");
out.write(fileData + "returned zero results \n");
out.flush();
} else {
out.write(fileData + "\n");
int i = 1;
String spaceString = " ";
while (keyItr.hasNext()) {
key1 = keyItr.next();
for (int k = 0; k < 40 - key1.length(); k++) {
spaceString = spaceString + " ";
}
total = total + sortedMapDesc.get(key1);
out.write(i + ":" + "'" + filename + "'" + ":" + "'" + seed + "'" + ":" + "'" + key1.replaceAll("'", "") + "'" + ":" + sortedMapDesc.get(key1) + ":" + "'" + "ADJ" + "'" + ":" + "'" + before_after + "'" + "\n");
//Code to add to the databases
preparedStatement = conn.prepareStatement(insertTableSQL);
preparedStatement.setString(1, filename);
preparedStatement.setString(2, seed);
preparedStatement.setString(3, key1);
if (sortedMapDesc.get(key1) != null) {
preparedStatement.setInt(4, sortedMapDesc.get(key1));
} else {
preparedStatement.setInt(4, 0);
}
preparedStatement.setString(5, "ADJ");
preparedStatement.setString(6, before_after);
System.out.println("Checking Prepared Statement:" + preparedStatement);
preparedStatement.executeUpdate();
System.out.println("Record Inserted :| ");
preparedStatement.close();
//System.out.println(out.toString());
i++;
spaceString = " ";
}
out.flush();
}
}
}
conn.close();
}
Well this is probably the first problem:
InputStreamReader reader1 = new InputStreamReader(in);
That's loading the file using the platform default encoding, which may or may not be appropriate for the file in question.
Likewise later:
fstream = new FileWriter(fileName);
Again, that will use the platform default encoding.
Always be explicit about your encoding - UTF-8 is usually a good choice, if you're in a position to choose.
Next, work out where issues are actually coming up. Log the exact UTF-16 code units in your strings, as integers, and try to spot when they go from "good" to "bad" (if they're ever good in the first place). See my blog post on diagnosing this sort of issue for more details. Something like this is useful:
public static void dumpString(String text) {
for (int i = 0; i < text.length(); i++) {
int codeUnit = text.charAt(i);
System.out.printf("%d: %c %04x%n", i, (char) codeUnit, codeUnit);
}
}
(Adjust to your logging infrastructure etc, of course.)

Update MySQL table using data from a text file through Java

I have a text file with four lines, each line contains comma separated values like below file
My file is:
Raj,raj34#myown.com,123455
kumar,kumar#myown.com,23453
shilpa,shilpa#myown.com,765468
suraj,suraj#myown.com,876567
and I have a MySQL table which contains four fields
firstname lastname email phno
---------- ---------- --------- --------
Raj babu raj34#hisown.com 2343245
kumar selva kumar#myown.com 23453
shilpa murali shilpa#myown.com 765468
suraj abd suraj#myown.com 876567
Now I want to update my table using the data in the above text file through Java.
I have tried using bufferedReader to read from the file and used split method using comma as delimiter and stored it in array. But it is not working. Any help appreciated.
This is what I have tried so far
void readingFile()
{
try
{
File f1 = new File("TestFile.txt");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String strln = null;
strln = br.readLine();
while((strln=br.readLine())!=null)
{
// System.out.println(strln);
arr = strln.split(",");
strfirstname = arr[0];
strlastname = arr[1];
stremail = arr[2];
strphno = arr[3];
System.out.println(strfirstname + " " + strlastname + " " + stremail +" "+ strphno);
}
// for(String i : arr)
// {
// }
br.close();
fr.close();
}
catch(IOException e)
{
System.out.println("Cannot read from File." + e);
}
try
{
st = conn.createStatement();
String query = "update sampledb set email = stremail,phno =strphno where firstname = strfirstname ";
st.executeUpdate(query);
st.close();
System.out.println("sampledb Table successfully updated.");
}
catch(Exception e3)
{
System.out.println("Unable to Update sampledb table. " + e3);
}
}
and the output i got is:
Ganesh Pandiyan ganesh1#myown.com 9591982389
Dass Jeyan jeyandas#myown.com 9689523645
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Gowtham Selvan gowthams#myown.com 9894189423
at TemporaryPackages.FileReadAndUpdateTable.readingFile(FileReadAndUpdateTable.java:35)
at TemporaryPackages.FileReadAndUpdateTable.main(FileReadAndUpdateTable.java:72)
Java Result: 1
#varadaraj:
This is the code of yours....
String stremail,strphno,strfirstname,strlastname;
// String[] arr;
Connection conn;
Statement st;
void readingFile()
{
try {
BufferedReader bReader= new BufferedReader(new FileReader("TestFile.txt"));
String fileValues;
while ((fileValues = bReader.readLine()) != null)
{
String[] values=fileValues .split(",");
strfirstname = values[0];
// strlastname = values[1];
stremail = values[1];
strphno = values[2];
System.out.println(strfirstname + " " + strlastname + " " + stremail +" "+ strphno);
}
bReader.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
// for(String i : arr)
// {
// }
try
{
st = conn.createStatement();
String query = "update sampledb set email = stremail,phno =strphno where firstname = strfirstname ";
st.executeUpdate(query);
st.close();
System.out.println("sampledb Table successfully updated.");
}
catch(Exception e3)
{
System.out.println("Unable to Update sampledb table. " + e3);
}
}
What you are having looks like a CSV file, you may consider libraries like Super CSV to help you in reading and parsing the file.
you are getting ArrayIndexOutOfBoundException upon trying to access at index 1 , i.e at lastname field value, so check whether you have no data at index 1 for any of the list elements in your text file
try this
public class FileReaderTesting {
static String stremail;
static String strphno;
static String strfirstname;
static String strlastname;
static Connection conn;
static Statement st;
public static void main(String[] args) {
try {
BufferedReader bReader= new BufferedReader(new FileReader("C:\\fileName.txt"));
String fileValues;
while ((fileValues = bReader.readLine()) != null)
{
String[] values=fileValues .split(",");
strfirstname = values[0];
// strlastname = values[1];
stremail = values[1];
strphno = values[2];
System.out.println(strfirstname + " " + stremail +" "+ strphno);
st = conn.createStatement();
String query = "update sampledb set email = '"+stremail+"',pno = '"+strphno+"' where firstname = '"+strfirstname+"' ";
System.out.println(query);
st.executeUpdate(query);
st.close();
System.out.println("sampledb Table successfully updated.");
}
bReader.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
catch(Exception e3)
{
System.out.println("Unable to Update sampledb table. " + e3);
}
}
}

Categories

Resources