Java Heap Memory Exception - java

I am reading some huge data from some csv files and putting into database(Mongodb). But after reading some of the files i am getting heap memory error in java.I don't understand what is wrong with my program.The below is my program -
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import au.com.bytecode.opencsv.CSVReader;
public class Csvread {
MongoClient mongoClient = null;
DB db = null;
DBCollection coll =null;
static Date myDate = new Date();
public Csvread()
{
try {
mongoClient = new MongoClient("localhost" ,27017);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db = mongoClient.getDB( "mahout" );
coll = db.getCollection("data_flowers_all");
DBObject fields=new BasicDBObject("product_url",1);
DBObject options=new BasicDBObject("unique",true);
// options.put("dropDups", true);
//
coll.createIndex(fields,options);
coll.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
}
public static void main(String[] args) {
String parentPath="/media/bqadmin/D87CFB1B7CFAF35C/flowersjack/csv files";
File parentFolder = new File(parentPath);
String[] files = parentFolder.list();
Csvread cvobj= new Csvread();
try {
for(String file : files)
{
System.out.println();
System.out.println();
System.out.println();
System.out.println("files are:" +parentPath+File.separator+file);
CSVReader reader = new CSVReader(new FileReader(parentPath+File.separator+file),',','"');
String [] nextLine;
try {
while ((nextLine = reader.readNext()) != null &&nextLine.length!=0) {
Encapsulation encap=new Encapsulation();
// System.out.println("product name"+nextLine[1]);
encap.setId(nextLine[0]);
encap.setProduct_name(nextLine[1]);
encap.setProduct_url(nextLine[6]);
encap.setProduct_image(nextLine[3]);
encap.setProduct_price(nextLine[5]);
encap.setProduct_src("www.flowersus.com");
encap.setCountry("USA");
encap.setDate(myDate);
encap.setCategory(nextLine[8]);
cvobj.DBConnection(encap);
}
} catch (IOException e) {
System.out.println("reading exception");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("closing reader");
e.printStackTrace();
}
System.out.println("inside forloop");
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("filenotfound");
e.printStackTrace();
}
}
public void DBConnection(Encapsulation enc ) throws IOException{
try {
System.out.println("Inside database connectivity");
BasicDBObject document=new BasicDBObject();
// System.out.println("BasicDBObject created");
document.put("product_id",enc.getId());
// System.out.println("product_id is" +enc.getId());
document.put("product_name",enc.getProduct_name() );
// System.out.println("product_name is" +enc.getProduct_name());
document.put("product_url",enc.getProduct_url());
System.out.println("product_url is" +enc.getProduct_url());
document.put("product_img", enc.getProduct_image());
// System.out.println("product_img is" +enc.getProduct_image());
document.put("product_price",enc.getProduct_price());
// System.out.println("price is" +enc.getProduct_price());
document.put("country", "India");
// System.out.println("country" +enc.getCountry());
document.put("date",new SimpleDateFormat("MM-dd-yyyy").format(myDate));
document.put("category", enc.getCategory());
// System.out.println("categoriy is" +enc.getCategory());
System.out.println(enc);
System.gc();//for clearing the object
coll.insert(document);
System.out.println("insertion complete");
}
catch (MongoException e) {
System.out.println("duplicate");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
In this i have called System.gc(); to clear objects...Can anyone tell what is wrong with my program.Any help will be highly appreciable.....
public class Encapsulation {
private String id;
private String product_name;
private String product_url;
private String product_image;
private String product_price;
private String product_src;
private String country;
private Date date;
private String Category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getProduct_url() {
return product_url;
}
public void setProduct_url(String product_url) {
this.product_url = product_url;
}
public String getProduct_image() {
return product_image;
}
public void setProduct_image(String product_image) {
this.product_image = product_image;
}
public String getProduct_price() {
return product_price;
}
public void setProduct_price(String product_price) {
this.product_price = product_price;
}
public String getProduct_src() {
return product_src;
}
public void setProduct_src(String product_src) {
this.product_src = product_src;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Date getDate() {
return date;
}
#Override
public String toString() {
return "Encapsulation [id=" + id + ", product_name=" + product_name + ", product_url=" + product_url
+ ", product_image=" + product_image + ", product_price=" + product_price + ", product_src="
+ product_src + ", country=" + country + ", date=" + date + ", Category=" + Category + "]";
}
public void setDate(Date myDate) {
this.date = myDate;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}
}
Please note:after inserting data for some time ,the heap memory issue arises.

I am not sure, but it could be a memory leak in the Java-MongoDB-driver.
One possible way to resolve your issue and/or increase the performance of your code. Use:
public WriteResult insert(List<DBObject> list)
or any other method, which allows to add multiple data records at once.
(http://api.mongodb.org/java/2.13/)
I know, that u will have problems figuring out, what the duplicate entries are this way. I am not such much into MongoDB to know the best way to handle duplicate entries when adding multiple data records at once. But in your case, i definitly would try that approach.
The second thing:
Do the standard stuff to handle heap errors. Increase at least your heap size.
I dont know, how big your string arrays get, but it could be, that they make your too small heap explode. Especially with a bad implementation of CSVReader and a slow garbage collector.
Thats all guessing, but what isnt, when it comes to memory leaks... :)
Hope it helps.

Related

How to process JSON Array nested inside an Array in java

Here is a java class CreateDoc which is sent from One web service that is producer side to another web service which is consumer side as List with content-type:Json
Below is the class representation
class CreateDoc{
DocMetData dMetaData;
DocContent dCont;
}
Class DocMetData {
String docNamel
String docType;
}
Class DocContent {
String data;
}
Once i receive the List as json in the consumer side i am not able to use this as a java Object and the content type is array with json nested inside an array.
Below is the Representation:
[
[
{
"dMetaData":{
"docName":"string",
"docType":"pdf"
},
"dCont":{
"data":"abc"
}
},
{
"dMetaData":{
"docName":"string",
"docType":"pdf"
},
"dCont":{
"data":"def"
}
},
{
"dMetaData":{
"docName":"string",
"docType":"pdf"
},
"dCont":{
"data":"ghk"
}
}
]
]
Question is how to process this and be able to use the data and represent as List.
Here's some sample code that shows how you can use the Jackson ObjectMapper to parse the data. Note that the code assumes the data is stored in a file, you can modify it as needed to suit your needs.
Here's the main class:
package parsing.arrayofarray;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ArrayOfArray {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String data = null;
try {
data = new String(Files.readAllBytes(Paths.get("src/main/resources/jsonArrayOfArray.json")));
} catch (IOException e1) {
e1.printStackTrace();
}
List<List<CreateDoc>> results = null;
try {
results = mapper.readValue(data, new TypeReference<List<List<CreateDoc>>>(){});
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(results);
}
}
and here are the supporting classes, first CreateDoc:
package parsing.arrayofarray;
public class CreateDoc {
DocMetData dMetaData;
DocContent dCont;
public DocMetData getdMetaData() {
return dMetaData;
}
public void setdMetaData(DocMetData dMetaData) {
this.dMetaData = dMetaData;
}
public DocContent getdCont() {
return dCont;
}
public void setdCont(DocContent dCont) {
this.dCont = dCont;
}
#Override
public String toString() {
return "CreateDoc [dMetaData=" + dMetaData + ", dCont=" + dCont + "]";
}
}
and DocContent:
package parsing.arrayofarray;
public class DocContent {
#Override
public String toString() {
return "DocContent [data=" + data + "]";
}
String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
and the DocMetData:
package parsing.arrayofarray;
public class DocMetData {
String docName;
String docType;
public String getDocNamel() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
#Override
public String toString() {
return "DocMetData [docNamel=" + docName + ", docType=" + docType + "]";
}
public String getDocType() {
return docType;
}
public void setDocType(String docType) {
this.docType = docType;
}
}
The output from the println is:
[[CreateDoc [dMetaData=DocMetData [docNamel=string, docType=pdf], dCont=DocContent [data=abc]], CreateDoc [dMetaData=DocMetData [docNamel=string, docType=pdf], dCont=DocContent [data=def]], CreateDoc [dMetaData=DocMetData [docNamel=string, docType=pdf], dCont=DocContent [data=ghk]]]]
You can use JSONArray(org.json) to parse the first list, and parse with GSON the inside list to create a List of CreatDoc. You can use only GSON to parse the first array too
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
public class Deserializer {
public static void main(String[] args) {
JSONArray jsonArray = new JSONArray(
"[[{\"dMetaData\": {\"docName\": \"string\",\"docType\": \"pdf\"},\"dCont\": {\"data\": \"abc\"}},{\"dMetaData\": {\"docName\": \"string\",\"docType\": \"pdf\"},\"dCont\": {\"data\": \"def\"}},{\"dMetaData\": {\"docName\": \"string\",\"docType\": \"pdf\"},\"dCont\": {\"data\": \"ghk\"}}]]");
JSONArray docsArray = jsonArray.getJSONArray(0);
List<CreateDoc> docsList = new Gson().fromJson(docsArray.toString(),
new TypeToken<ArrayList<CreateDoc>>() {}.getType());
docsList.forEach(System.out::println);
}
public static class CreateDoc {
DocMetData dMetaData;
DocContent dCont;
#Override
public String toString() {
return this.dMetaData.toString() + " " + this.dCont.toString();
}
}
public static class DocMetData {
String docName;
String docType;
#Override
public String toString() {
return "name: " + this.docName + " type: " + this.docType;
}
}
public static class DocContent {
String data;
#Override
public String toString() {
return "data: " + this.data;
}
}
}
You can use GSON to parse the message into a JSONArray with JSONObjects. Then create a parser for each class to convert the fields from the JSONObject into Java objects. Similar question is anwered here.
I think the problem is you are trying to map json to CreateDoc instead of CreateDoc List. If you are using spring boot to manage rest layer in your application use #Requestbody List CreateDoc in the method to convert your json. This will use Jackson converter internally. Otherwise you can use Jackson converter jar to convert your json to objects.

getting null values when trying to pass values to xml response in web service from model class

/**
* ConnectDB2.java , i'm fetching data from database and setting values to model class.
*/
package org.com.repair.spotify.repair.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.ws.rs.Path;
import org.com.repair.spotify.repair.model.RepairDetails;
/**
* #author www.javaworkspace.com
*
*/
#Path("/connectDB2")
public class ConnectDB2 {
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
String deviceName;
String deviceModel;
String ticketId;
String issue;
String deviceType;
public ConnectDB2() {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
connection = DriverManager.getConnection(
"jdbc:db2://localhost:50000/HELLO", "db2admin", "admin");
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM DEVICE ");
while (resultSet.next()) {
System.out.println("DEVICE BRAND:" + resultSet.getString(1)
+ " || ISSUE: " + resultSet.getString(2) + " ||MODEL:"
+ resultSet.getString(3) + "||TYPE:"
+ resultSet.getString(4));
RepairDetails Rd = new RepairDetails();
Rd.setDeviceModel(resultSet.getString(1));
Rd.setIssue(resultSet.getString(2));
Rd.setDeviceType(resultSet.getString(3));
Rd.setDeviceType(resultSet.getString(4));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/RepairDetails.java==> my Model class/
package org.com.repair.spotify.repair.model;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class RepairDetails {
String deviceName;
String deviceModel;
String ticketId;
String issue;
String deviceType;
public RepairDetails() {
}
public RepairDetails(String deviceName, String deviceModel,
String ticketId, String issue, String deviceType) {
super();
this.deviceName = deviceName;
this.deviceModel = deviceModel;
this.ticketId = ticketId;
this.issue = issue;
this.deviceType = deviceType;
}
public String getDeviceName() {
System.out.println("getter" + deviceName);
return deviceName;
}
public void setDeviceName(String deviceName) {
System.out.println("setter" + deviceName);
this.deviceName = deviceName;
}
public String getDeviceModel() {
return deviceModel;
}
public void setDeviceModel(String deviceModel) {
System.out.println("setter" + deviceModel);
this.deviceModel = deviceModel;
}
public String getTicketId() {
return ticketId;
}
public void setTicketId(String ticketId) {
this.ticketId = ticketId;
}
public String getIssue() {
return issue;
}
public void setIssue(String issue) {
System.out.println("setter" + issue);
this.issue = issue;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
System.out.println("setter" + deviceType);
this.deviceType = deviceType;
}
}
//the service class from where i'm trying get values from model, but i'm fetching null value which is further passed on getRepairdetails()
package org.com.repair.spotify.repair.service;
import java.util.ArrayList;
import java.util.List;
import org.com.repair.spotify.repair.db.ConnectDB2;
import org.com.repair.spotify.repair.model.*;
public class RepairService {
public RepairService() {
ConnectDB2 db = new ConnectDB2();
}
public List<RepairDetails> getRepairService()
{
System.out.println("getRepairDetails-->2");
RepairDetails Rd = new RepairDetails();
System.out.println("hey im firing");
RepairDetails RD1 = new RepairDetails(Rd.getDeviceName(),
Rd.getDeviceModel(), Rd.getIssue(), Rd.getDeviceType(),
"Mobile");
List<RepairDetails> list = new ArrayList<RepairDetails>();
list.add(RD1);
return list;
}
}
Kindly help me why null values are returned by getter ???
Let's start with examining the RepairDetails class. This one implements a POJO (Plain old Java object) and contains two constructors.
public RepairDetails()
public RepairDetails(String, String, String, String, String)
So when you create the object with the first constructor, that means you are not setting anything to the fields, that means that the values are initialized to null.
Now let's examine the RepairService class. There you have this code in the getRepairService() method.
RepairDetails Rd = new RepairDetails();
RepairDetails RD1 = new RepairDetails(Rd.getDeviceName(),
Rd.getDeviceModel(), Rd.getIssue(), Rd.getDeviceType(),
"Mobile");
List<RepairDetails> list = new ArrayList<RepairDetails>();
list.add(RD1);
Here we have the following observations:
Rd is an object created with the first constructor, so effectively the values in Rd will be null.
You are constructing RD1 with values got from Rd meaning that they will be null too.
I hope you get it now.

ArrayList becomes null while writing to file

My arraylist<"obj"> becomes null after trying to write to a file.
In WriteToFile class arraylist info becomes null after executing the last line
writer.write(info.get(i).getIpadd().toString()+"\n");
It works on the first instance when i am writing another list to file but does not when i run it the 2nd instance. I dun understand why its happening. Below is the whole code and the stack trace.
WriteToFile Class:
public class WriteToFile {
public WriteToFile(ArrayList<Information> info,String location)
{
FileWriter writer=null;
try
{
writer = new FileWriter(location);
System.out.println(info.size());
for(int i=0;i<info.size()-1;i++)
{
writer.write(info.get(i).getDate().toString()+",");
writer.write(info.get(i).getAccount().toString()+",");
writer.write(info.get(i).getStatus().toString()+",");
writer.write(info.get(i).getIpadd().toString()+"\n");
System.out.println(info.get(i).getAccount());
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
finally
{
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
StackTrace:
java.lang.NullPointerException
at WriteToFile.<init>(WriteToFile.java:17)
at Gen_Report.<init>(Gen_Report.java:45)
at Gen_Report.main(Gen_Report.java:57)
main:
public class Gen_Report {
public Gen_Report()
{
// TODO Auto-generated constructor stub
//locate file and read all text from .log file to .csv
//file is found.so read text from it and extract all date/time, email add ,accepts and rejects,ip add, delete
Date date=new Date();
String[] dateTokens=date.toString().split(" ");
String dateString=dateTokens[2]+dateTokens[1]+dateTokens[5]+"_"+dateTokens[3].substring(0, 2)+dateTokens[3].substring(3,5)+dateTokens[3].substring(6, 8);
String logFileLocation = "/Users/gundu_87/Documents/workspace/GenFLRReport/";
ReaderFromLog rfl = new ReaderFromLog(logFileLocation+"radsecproxy.log");
//include duplicates
WriteToFile wtf = new WriteToFile(rfl.log,logFileLocation+dateString+"_FLRlogduplicates.txt");
//exclude duplicates
RemoveDuplicatesInList rdil = new RemoveDuplicatesInList(logFileLocation+dateString+"_FLRlogduplicates.txt");
for(int i=0;i<rdil.log.size();i++)
{
System.out.println(rdil.log.get(i).getAccount());
}
wtf = new WriteToFile(rdil.log,logFileLocation+dateString+"_FLRlog.txt");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Gen_Report gr= new Gen_Report();
}
}
Information class:
public class Information {
private String ipadd;
private String status;
private String account;
private String date;
public String getIpadd() {
return ipadd;
}
public void setIpadd(String ipadd) {
this.ipadd = ipadd;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
ReaderFromLog class
public class ReaderFromLog {
Scanner s1 = null;
String line=null;
ArrayList<Information> log;
public ReaderFromLog(String logFileLocation) {
// TODO Auto-generated constructor stub
File logFile = new File(logFileLocation);
if(!logFile.exists())
{
System.err.println("File not found");
System.exit(1);
}
else
{
try
{
s1 = new Scanner(new FileReader(logFile));
} catch (FileNotFoundException e)
{
System.err.print("File not found");
}
}
log=new ArrayList<Information>();
//store into a array
//exclude any repeats
do{
line=s1.nextLine();
Information newUser = new Information();
if(line.contains("Access-Accept for user"))
{
newUser.setStatus("Accept");
String[] sb=line.split(" ");
newUser.setAccount(sb[7]);
int idx_Ipadd = 0;
for(int i=0;i<sb.length;i++)
if (sb[i].contentEquals("to"))
idx_Ipadd=i;
newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
log.add(newUser);
}
else if(line.contains("Access-Reject for user"))
{
newUser.setStatus("Reject");
String[] sb=line.split(" ");
newUser.setAccount(sb[7]);
int idx_Ipadd = 0;
for(int i=0;i<sb.length;i++)
if (sb[i].contentEquals("to"))
idx_Ipadd=i;
newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
log.add(newUser);
}
}while(s1.hasNextLine());
}
}
RemoveDuplicate class:
public class RemoveDuplicatesInList {
Scanner s1 = null;
String line=null;
ArrayList<Information> log;
public RemoveDuplicatesInList(String duplicateFileLocation)
{
// TODO Auto-generated constructor stub
File logFile = new File(duplicateFileLocation);
if(!logFile.exists())
{
System.err.println("File not found");
System.exit(1);
}
else
{
try
{
s1 = new Scanner(new FileReader(logFile));
} catch (FileNotFoundException e)
{
System.err.print("File not found");
}
}
log=new ArrayList<Information>();
//store into a array
//exclude any repeats
do{
boolean sameAccount=false;
line=s1.nextLine();
Information newUser = new Information();
if(line.contains("Accept"))
{
newUser.setStatus("Accept");
String[] sb=line.split(",");
sameAccount=false;
for(int i=0;i<log.size();i++)
if(log.get(i).getAccount().contentEquals(sb[1]))
{
sameAccount=true;
break;
}
if(!sameAccount)
{
newUser.setAccount(sb[1]);
newUser.setIpadd(sb[3]);
newUser.setDate(sb[0]);
log.add(newUser);
}
}
else if(line.contains("Reject"))
{
newUser.setStatus("Reject");
String[] sb=line.split(",");
for(int i=0;i<log.size();i++)
if(log.get(i).getAccount().contentEquals(sb[1]))
{
sameAccount=true;
break;
}
if(!sameAccount)
{
newUser.setAccount(sb[1]);
newUser.setIpadd(sb[3]);
newUser.setDate(sb[0]);
log.add(newUser);
}
}
}while(s1.hasNextLine());
}
}
Check value of
info.get(i).getIpadd()
if value of this is null then .toString(0 will give you NullPointerException

Can xstream deserialize a complicated array?

I study xstream these days.
But I found the xstream json tutorial which in its homepage is very simple.
I have an array as follows:
{
"mails":[
{
"uid":"ZC2027-mXOmcAtkfiztS0sEeJlkU25",
"relatedCardNums":"8299,0000,1531|8299,0000,1531",
"houseHolder":"",
"subject":"no-subject",
"receiveTime":"2012-05-27 00:00:00",
"bankName":"上海银行",
"cards":[]
}
],
"dealedNum":330,
"nextRequestDelay":"1",
"percent":"0",
"filterNum":410,
"resCode":"01",
"dealedBillNum":43,
"resMsg":"正在解析"
}
I want to convert this json string to a GetMailsDataResponseDto, but I dont know how to do?
Could you help me out?
package com.fund.etrading.ebankapp.base.credit.cardniu.ecardniu.dto;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fund.etrading.ebankapp.base.credit.utils.FileUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class GetMailsDataResponseDto extends ResponseBaseDto{
protected int dealedNum;
protected String nextRequestDelay;
protected String percent;
protected int filterNum;
protected int dealedBillNum;
protected List mails = new ArrayList();
public List getMails() {
return mails;
}
public int getDealedNum() {
return dealedNum;
}
public String getNextRequestDelay() {
return nextRequestDelay;
}
public String getPercent() {
return percent;
}
public int getFilterNum() {
return filterNum;
}
public int getDealedBillNum() {
return dealedBillNum;
}
public void fromJson(String json){
try {
json = FileUtils.get_content("C:\\Documents and Settings\\Administrator\\workspace\\99fund_java\\src\\com\\fund\\etrading\\ebankapp\\base\\credit\\新建 文本文档 (2).txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
json = "{\"root\":" + json + "}";
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("root", this.getClass());
//xstream.addImplicitCollection(this.getClass(), "mails");
xstream.alias("mail", MailDto.class);
//xstream.aliasField("cards", MailDto.class, "cards");
//xstream.aliasField("currencyData", CardDto.class, "currencyData");
//xstream.aliasField("data", CurrencyDataDto.class, "data");
xstream.fromXML(json, this);
}
}
package com.fund.etrading.ebankapp.base.credit.cardniu.ecardniu.dto;
import java.util.ArrayList;
import java.util.List;
import com.fund.etrading.ebankapp.base.credit.BaseDto;
public class MailDto extends BaseDto{
protected String uid;
protected String relatedCardNums;
protected String houseHolder;
protected String subject;
protected String receiveTime;
protected String bankName;
protected List cards = new ArrayList();
public String getUid() {
return uid;
}
public String getRelatedCardNums() {
return relatedCardNums;
}
public String getHouseHolder() {
return houseHolder;
}
public String getSubject() {
return subject;
}
public String getReceiveTime() {
return receiveTime;
}
public String getBankName() {
return bankName;
}
public List getCards() {
return cards;
}
}
thanks in advance!
If you want to convert json string to your custom class(ex.GetMailsDataResponseDto), I recommend Google Gson.
If you use Gson, yon don't need fromJosn() method in GetMailsDataResponseDto class.
If you only use json parsing and have experiences of java script, I recommend Djson parser(java library).
"Djson Parse version 0.8a" -- http://blog.indf.net/category/Apps/djson
j1.txt - tip: "none BOM & UTF-8"
....
public void fromJson(String json){
//(real-code)--start
//Var var = Djson.parse(json);
//(real-code)--end
//--test-code--start
Var var = null;
try {
var = Djson.parse(new File("d:\\j1.txt"));
} catch (IOException e) {
e.printStackTrace();
}
//--test-code--end
this.dealedNum = var.get("dealedNum").toInt();
this.nextRequestDelay = var.get("nextRequestDelay").toString();
this.percent = var.get("percent").toString();
this.filterNum = var.get("filterNum").toInt();
this.dealedBillNum = var.get("dealedBillNum").toInt();
for(int i=0; i<var.get("mails").size(); i++) {
this.mails.add(var.get("mails").get(i).toObject()); // MAP type setting...
}
}

Java HashMap Create, edit and delete

I am trying to update a HashMap use it directly in the next method, but it isn't working. From what I read I couldn't find a solution. Some say'd it is impossible and some say use an iterator, but even with the iterator it's not working. the error is the printing method it is not printing or even getting inside the while loop because it is empty but i cant find why
This is the two methods I'm trying to update and print some information.
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class OrderList {
// Storage for an arbitrary number of details.
private HashMap<String, Order> orderList = new HashMap<String, Order>();
/**
* Perform any initialization .
*/
public OrderList() {
orderList = new HashMap<String, Order>();
}
public HashMap<String, Order> getOrders() {
return orderList;
}
public void readOrderFile(String OrderListPath) {
try {
File file = new File(OrderListPath);
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String readLine = scan.nextLine();
if (readLine != null) {
getSplitLinesOrders(readLine);
}
}
} catch (Exception e) {
}
}
public void getSplitLinesOrders(String readLine) {
String id = "";
String customerId = "";
String itemId = "";
int quantity = 0;
try {
String[] splitedLine = readLine.split(",");
if (splitedLine.length == 4) {
id = splitedLine[0];
customerId = splitedLine[1];
itemId = splitedLine[2];
quantity = Integer.parseInt(splitedLine[3]);
Order newOrder = new Order(id, customerId, itemId, quantity);
orderList.put(id, newOrder);
}
} catch (Exception e) {
}
}
/**
* Add a new set of details to the list
* #param details The details of the staff
*/
// public void addDetails(Order details) {
// orderList.add(details);
// }
public boolean hasOrder() {
return orderList.size() != 0;
}
public Order getNextOrder() {
Order order = orderList.remove(0);
return order;
}
/**
* #return All the details
*/
public String listDetails() {
StringBuffer allEntries = new StringBuffer();
for (Map.Entry<String, Order> details : orderList.entrySet()) {
String Key = details.getKey();
Object value = details.getValue();
allEntries.append(Key + " " + value);
}
return allEntries.toString();
}
public void PrintListOfOrders() {
Iterator it = getOrders().entrySet().iterator();
try {
while (it.hasNext()) {
Order value = (Order) it.next();
System.out.println(value.getOrderId() + " " + value.getCustomerId() + " " + value.getItemId() + " " + value.getQuantity());
}
} catch (Exception e) {
System.out.println(e);
}
}
}
You're probably getting a NullPointerException? Next time tell us what is going wrong and provide stacktraces if applicable.
The code you posted doesn't create an instance of orderList, so if it's not done elsewhere that code will throw a NullPointerException
Try adding:
private HashMap<String, Order> orderList = new HashMap<String, Order>;
Swallowing an Exception like this:
} catch (Exception e) {
}
is not a good practice since it will hide all information about what's going wrong, at least do:
catch (Exception e) {
e.printStacktrace();
}
You could do something like this:
Set<String> s = List.keySet();
Iterator<String> i = s.iterator();
Which is the initialization of the iterator, and then you could iterate through the keys using i.next(), getting a String each time and asking orderList.get(thatString) to get the value.

Categories