This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am facing some problem in this function where I'm running into null values and getting a null pointer exception. sqlDate and sqlTime are null.
private void setCalendarComboDetails(Map tenantConnInfo, HttpSession session, HttpServletRequest request) {
logger.info("Enter setCalendarComboDetails");
Appointment appointmentInfo = new Appointment();
Date sqlDate;
Time sqlTime;
try {
CompanyDbImpl companyImpl = new CompanyDbImpl(tenantConnInfo);
PatientDbImpl patientImpl = new PatientDbImpl(tenantConnInfo);
DoctorDbImpl doctorImpl = new DoctorDbImpl(tenantConnInfo);
CalendarDbImpl calendarImpl = new CalendarDbImpl(tenantConnInfo);
ConfigurationDbImpl configurationImpl = new ConfigurationDbImpl(tenantConnInfo);
session.setAttribute("isvalid",true);
session.removeAttribute("appointmentInfo");
// session.removeAttribute("index");
List<CompanyBranchDetails> branchDetailsList = companyImpl.getCompanyNBranchList();
session.setAttribute("companyBranchList", branchDetailsList);
List<PatientInfo> patientList = patientImpl.getPatientInfoList();
session.setAttribute("patientInfoList", patientList);
String whereClause = " dbd.branch_id=" + branchDetailsList.get(0).getBranchId();
List<DoctorBranchDetails> doctorBranchDetailsList = doctorImpl.getDoctorBranchDetailsListByWhereClause(whereClause);
System.out.println("doctor branch list size " + doctorBranchDetailsList.size());
session.setAttribute("doctorBranchList", doctorBranchDetailsList);
Map configMap = configurationImpl.getConfigurationMapByConfigType(ApplicationConstants.CONFIGURATION_TYPE_CALENDAR);
int timeFormatId = Integer.parseInt(configMap.get("time_format_id").toString());
whereClause = " time_format_id=" + timeFormatId;
String dbTimeFormat = calendarImpl.getTimeFormatListByWhereClause(whereClause).get(0).getTimeFormatType();
int dateFormatId = Integer.parseInt(configMap.get("date_format_id").toString());
whereClause = " date_format_id=" + dateFormatId;
String dbDateFormat = calendarImpl.getDateFormatListByWhereClause(whereClause).get(0).getDateFormatType();
session.setAttribute("calendarDateFormat", dbDateFormat);
String jsStart = request.getParameter("start1");
String jsEnd = request.getParameter("end1");
String jsDateNTimeFormat = ApplicationConstants.JS_DATENTIME_PATTERN;
System.out.println("start1 " + request.getParameter("start1") + " " + new java.util.Date());
sqlDate = appointmentInfo.getStartDate().getDatepickerDate();
appointmentInfo.setStrStartDate(ApplicationUtils.formatSqlDate(dbDateFormat, sqlDate));
System.out.println("sqlDate1"+sqlDate);
sqlTime = appointmentInfo.getStartTime().getTimepickerTime();
appointmentInfo.setStrStartTime(ApplicationUtils.formatSqlTime(dbTimeFormat, sqlTime));
java.util.Date date1 = ApplicationUtils.getDateFromSqlDateNTime(sqlDate, sqlTime);
sqlDate = appointmentInfo.getEndDate().getDatepickerDate();
appointmentInfo.setStrEndDate(ApplicationUtils.formatSqlDate(dbDateFormat, sqlDate));
sqlTime = appointmentInfo.getEndTime().getTimepickerTime();
appointmentInfo.setStrEndTime(ApplicationUtils.formatSqlTime(dbTimeFormat, sqlTime));
java.util.Date date2 = ApplicationUtils.getDateFromSqlDateNTime(sqlDate, sqlTime);
String diff = ApplicationUtils.getDateDifference(date1, date2);
appointmentInfo.setDuration(diff);
System.out.println("difference"+diff);
if(session.getAttribute("index") != null) {
doctorBranchDetailsList = (ArrayList<DoctorBranchDetails>)session.getAttribute("doctorBranchDetailsList");
int selectedIndex = Integer.parseInt(session.getAttribute("index").toString());
DoctorBranchDetails doctorBranchInfo = doctorBranchDetailsList.get(selectedIndex);
appointmentInfo.getDoctorBranchDetails().setDoctorBranchId(doctorBranchInfo.getDoctorBranchId());
appointmentInfo.getDoctorBranchDetails().getCompanyBranchDetails().setBranchId(doctorBranchInfo.getCompanyBranchDetails().getBranchId());
appointmentInfo.getDoctorBranchDetails().setDoctorName(doctorBranchInfo.getDoctorInfo().getFullName());
appointmentInfo.getDoctorBranchDetails().setBranchNType(doctorBranchInfo.getBranchNType());
}
appointmentInfo.setStrStartDate(ApplicationUtils.converDateFormats(jsStart, jsDateNTimeFormat, dbDateFormat));
appointmentInfo.setStrStartTime(ApplicationUtils.converDateFormats(jsStart, jsDateNTimeFormat, dbTimeFormat));
appointmentInfo.setStrEndDate(ApplicationUtils.converDateFormats(jsEnd, jsDateNTimeFormat, dbDateFormat));
appointmentInfo.setStrEndTime(ApplicationUtils.converDateFormats(jsEnd, jsDateNTimeFormat, dbTimeFormat));
session.setAttribute("appointmentInfo", appointmentInfo);
} catch (Exception e) {
logger.error(e, e);
}
logger.info("Exit setCalendarComboDetails");
}
Learn to Debug.
You have many tools at your disposal. Try them. Figure this out. If you can't break apart your code and find problems, you won't be much use as a programmer on anything other than tiny applications. This site is not a replacement for a debugger. Here are some things you should work on.
Figure out the debugger yourself or use an IDE like Eclipse that has those functions built in. Set breakpoints at the start of the function and step through it. Make sure all variables contain what you want at each step of the way.
Add System.out.println() lines to the code or use a logging framework like log4j to output the contents of variables along the way. Review the console after running this part of the code and make sure that all variables contain what you want each step of the way.
Refactor the code into smaller methods. Unit test each one individually. This helps isolate the part of the code that is breaking from the parts that aren't.
Related
I'm trying to use JGit to pull a log of commits done by developers using a Github security token. I tried googling a lot, but the documentation is scarce on this. The code that I am currently tinkering with looks like this:
public class GitIntegration {
public GitIntegration() {
}
public Iterable<RevCommit> getCommits() throws IOException, InvalidRefNameException, GitAPIException {
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.setMustExist( true );
repositoryBuilder.setGitDir( new File ("https://oauth2:xyzxyzTOKENxyzxyz#github.com/myuser/myrepo.git"));
Repository repository = repositoryBuilder.build();
try (Git git = new Git(repository)) {
Iterable<RevCommit> commits = git.log().all().call();
int count = 0;
for (RevCommit commit : commits) {
System.out.println("LogCommit: " + commit);
count++;
}
System.out.println(count);
return commits;
}
}
}
But I am getting the following error:
java.nio.file.InvalidPathException: Illegal char <:> at index 5:
https:\oauth2:xyzxyzTOKENxyzxyz#github.com\myuser\myrepo.git\config
sun.nio.fs.WindowsPathParser.normalize(Unknown Source)
It doesn't work to throw git syntax in the File constructor like that at all. Does anyone know of a way to authenticate with tokens and then be able to use the git.log() method?
Thanks!
After digging around a bit, I decided to go with the Kohsuke github library instead. When doing that it was quite straightforward. Posting basic code example for reference. (Please note that this if for Enterprise Git. If you are doing it for "normal" Git, look into the Kohsuke documentation, you will need to use another method instead of connectToEnterpriseWithOAuth.)
import org.kohsuke.github.*;
public class GitIntegration {
public GitIntegration() {
}
public String getCommits(String repo) {
String retval = "";
try {
GitHub gitHub = GitHub.connectToEnterpriseWithOAuth("https://git.our.company.com/api/v3", "my.email#ourcompany.com", "xyzxyzTokenxyzxyz");
GHMyself me = gitHub.getMyself();
GHRepository repository = gitHub.getRepository("Gituser/"+repo);
Calendar cal = Calendar.getInstance();
cal.set(2020, 10, 4);
Date since = cal.getTime();
GHCommitQueryBuilder queryBuilder = repository.queryCommits().since(since).until(new Date());
PagedIterable<GHCommit> commits = queryBuilder.list();
Iterator<GHCommit> iterator = commits.iterator();
while (iterator.hasNext()) {
GHCommit commit = iterator.next();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
GHUser user = commit.getAuthor();
String name = "";
if(user != null) name = user.getName();
String message = commit.getCommitShortInfo().getMessage();
retval = retval.concat(message + " | " + name + " | " + sdf.format(commit.getCommitDate()) + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
retval = e.getCause().getMessage();
e.printStackTrace();
}
return retval;
}
}
So I grow up with C++ and have no problem with memory management whatsoever building C++ programs. But the time has come the market is asking me to understand and learn Java. Okay no problem! After spending a lot of time developing Java applications I keep hitting my head with this memory management in Java.
I took one of my applications and trying to understand how to keep a low heap size. The image bellow is a print-screen from VisualVM 2.0.1. The numbers represent the times I call the CollectData() function. As you can see, call number 1 is all okay, but when I call it again it grows with 125MB and keeps going on. I don't understand why? (So I paste my code bellow, so maybe someone could help me explaining whats going on).
So I tried basically everything with playing with the null and new operators, without any success. Also I tried playing with the System.gc() function.
public void CollectData() {
/// Variables
taBB.setText("");
Connection con;
String tb_name_hours = "table_name";
String tb_name_minutes = "table_name";
String db_name = "database_name";
List<TickerIntervalData> data_set_mins = new ArrayList<TickerIntervalData>();
List<TickerIntervalData> data_set_hours = new ArrayList<TickerIntervalData>();
DecimalFormat dec = new DecimalFormat("#0.0000");
DecimalFormat dec2 = new DecimalFormat("#0.00000000");
///
try {
/// Connect
String url = "jdbc:mysql://" + tfIP.getText() + ":" + tfPort.getText();
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
DriverManager.setLoginTimeout(2);
con = DriverManager.getConnection(url, tfUser.getText(), String.valueOf(tfPassword.getPassword()));
// Use db_name
Statement stt = con.createStatement();
stt.execute("USE " + db_name);
// Get rows from hour table
ResultSet res = stt.executeQuery("SELECT * FROM " + tb_name_hours);
while (res.next()) {
TickerIntervalData new_record = new TickerIntervalData();
new_record.setOpen(res.getDouble("open"));
new_record.setHighest(res.getDouble("highest"));
new_record.setLowest(res.getDouble("lowest"));
new_record.setClose(res.getDouble("close"));
new_record.setVolume(res.getDouble("volume"));
new_record.setBv(res.getDouble("bv"));
new_record.setTimestamp(res.getString("time"));
data_set_hours.add(new_record);
}
// Get rows from minute table
res = stt.executeQuery("SELECT * FROM " + tb_name_minutes);
while (res.next()) {
TickerIntervalData new_record = new TickerIntervalData();
new_record.setOpen(res.getDouble("open"));
new_record.setHighest(res.getDouble("highest"));
new_record.setLowest(res.getDouble("lowest"));
new_record.setClose(res.getDouble("close"));
new_record.setVolume(res.getDouble("volume"));
new_record.setBv(res.getDouble("bv"));
new_record.setTimestamp(res.getString("time"));
data_set_mins.add(new_record);
}
// Free resources
res.close();
stt.close();
res = null;
stt = null;
//
data_set_hours.clear();
data_set_hours = null;
data_set_mins.clear();
data_set_mins = null;
} catch (Exception e) {
taBB.setText(e.toString());
}
}
I am working with JPA, my web application is taking 60 sec to execute this method, I want to execute it faster how to achive ?
public boolean evaluateStudentTestPaper (long testPostID, long studentID, long howManyTimeWroteExam) {
Gson uday = new Gson();
Logger custLogger = Logger.getLogger("StudentDao.java");
// custLogger.info("evaluateTestPaper test paper for testPostID: " +
// testPostID);
long subjectID = 0;
// checking in table
EntityManagerFactory EMF = EntityManagerFactoryProvider.get();
EntityManager em = EMF.createEntityManager();
List<StudentExamResponse> studentExamResponses = null;
try {
studentExamResponses = em
.createQuery(
"SELECT o FROM StudentExamResponse o where o.studentId=:studentId And o.testPostID=:testPostID and o.howManyTimeWroteExam=:howManyTimeWroteExam")
.setParameter("studentId", studentID).setParameter("testPostID", testPostID)
.setParameter("howManyTimeWroteExam", howManyTimeWroteExam).getResultList();
System.out.println("studentExamResponses--------------------------------------------------"
+ uday.toJson(studentExamResponses) + "---------------------------------------");
} catch (Exception e) {
custLogger.info("exception at getting student details:" + e.toString());
studentExamResponses = null;
}
int studentExamResponseSize = studentExamResponses.size();
if (AppConstants.SHOWLOGS.equalsIgnoreCase("true")) {
custLogger.info("student questions list:" + studentExamResponseSize);
}
// Get all questions based on student id and test post id
List<ExamPaperRequest> examPaperRequestList = new ArrayList<ExamPaperRequest>();
List<Questions> questionsList = new ArrayList<Questions>();
// StudentExamResponse [] studentExamResponsesArgs =
// (StudentExamResponse[]) studentExamResponses.toArray();
// custLogger.info("Total questions to be evaluated: " +
// examPaperRequestList.size());
List<StudentTestResults> studentTestResultsList = new ArrayList<StudentTestResults>();
StudentTestResults studentTestResults = null;
StudentResults studentResults = null;
String subjectnames = "", subjectMarks = "";
int count = 0;
boolean lastIndex = false;
if (studentExamResponses != null && studentExamResponseSize > 0) {
// studentExamResponses.forEach(studentExamResponses->{
for (StudentExamResponse o : studentExamResponses.stream().parallel()) {
// 900 lines of coade inside which includes getting data from database Queries
}
}
As #Nikos Paraskevopoulos mentioned, it should probably be the ~900 * N database iterations inside that for loop.
I'd say to avoid DB iterations as much as you can, specially inside a loop like that.
You can try to elaborate your current StudentExamResponse sql to englobe more clauses - those you're using inside your for mainly, which could even diminish the amount of items you iterate upon.
My guess would be your select query is taking time.
If possible, set query timeout to less than 60 seconds & confirm this.
Ways of setting query timeout can be found out there - How to set the timeout period on a JPA EntityManager query
If this is because of query, then you may need to work to make select query optimal.
I am trying to generate an invoice number that contains name, branch and city with current date. I have gathered the information from JComboboxes and SimpleDateFormat. But in the end after we concat all the values and set them to a Jtextfield I get string index out of bound exception.
I am new to java and don't have much knowledge about it whatever efforts I have made for this are given as follows. I will be grateful to people interested in supporting me. Boots and Bouquets both are welcome.
private void bt_generateActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == bt_generate) {
if ((cb_bkname.getSelectedItem()
.equals("<html><font color=\"red\">SELECT NAME</font></html>"))
|| (cb_brname.getSelectedItem()
.equals("<html><font color=\"red\">SELECT BRANCH</font></html>"))
|| (cb_plname.getSelectedItem()
.equals("<html><font color=\"red\">SELECT PLACE</font></html>"))
){
} else {
String datePrefix = new SimpleDateFormat("MMMM dd, YYYY")
.format(new Date());
tf_rm_dt.setText(datePrefix);
Object name = cb_bkname.getSelectedItem();
String bn = name.toString().substring(0, 3);
Object branch = cb_brname.getSelectedItem();
String br = branch.toString().substring(0, 4);
Object city = cb_bkname.getSelectedItem();
String pl = city.toString().substring(0, 4);
String curdt = new SimpleDateFormat("dd-MM-YY")
.format(new Date());
tf_rm_id.setText("" + bn + "/" + br + "/" + "/" + curdt);
}
}
}
The StackTrace is as follows:
run:
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.substring(String.java:1955)
at myproj.DATAENTRY.bt_generateActionPerformed(DATAENTRY.java:2215)
at myproj.DATAENTRY.access$2300(DATAENTRY.java:20)
at myproj.DATAENTRY$24.actionPerformed(DATAENTRY.java:597)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
One of your strings is to short:
The error is either in one of these 3 lines
String bn=name.toString().substring(0, 3);
String br=branch.toString().substring(0, 4);
String pl=city.toString().substring(0, 4);
Print out the string, and check length before doing the substring()
This is an inefficient and overly complicated means to get information from a combo box! Can't you assign the selection, to an object (as an enum type, for example), prior to the concatenation? If you know what the selections are (which hopefully you do) you shouldn't ever need to use substring!
I'm trying to debug a method in Java using NetBeans.
That method is:
public Integer getNumberOfClamps(Locations paLocation) {
Integer ret = -1;
List list = new ArrayList();
String removeme = "ABC";
if (paLocation == null) {
return ret;
}
try {
IO io = new IO(this.getSchemaName());
Session session = io.getSession();
String sql = "select count(*) from assets a join assettypes at on (at.id = a.assettype_id) ";
sql += "where a.currentlocation_id = " + paLocation.getId() + " and at.clamp = 1 and at.active = 1;";
list = session.createQuery(sql).list();
// for some reason, list is empty yet MySQL reports 40 records
// and the following two lines are never reached!
ret = list.size();
removeme = "WHAT???";
} catch (Exception ex) {
ret = -1; // explicitly set return
} finally {
return ret;
}
}
Towards the middle of the method you will see list = session.createQuery(sql).list();
For some reason, this is returning an empty list even though when the SQL is run manually, I get 40 results.
But the odd part is that once the .list() is called, it jumps to the finally block and never reaches the rest! So for testing, 'removeme' should equal WHAT??? but the debugger reports it as still ABC.
What gives?
You are using the wrong method. 'createQuery' is expecting HQL syntax. Change your method to 'createSQLQuery'