I have an activity 'B'. I have 2 more activities A and C. Both the activities lead to B. But i pass different Data from A and C. So while fetching
String dataFromA = getIntent.getStringExtra("SomethingA");
String dataFromC = getIntent.getStringExtra("SomethingC");
How to not get an error. I wont know from where the user is getting to activity B So how do i add an If statement or seomthing to not get an error while fetching as Either line A or C will get a NullPOinterException
You can use hasExtra method to check if that String exists.
if (getIntent().hasExtra("SomethingA")) {
String dataFromA = getIntent.getStringExtra("SomethingA");
} else if (getIntent().hasExtra("SomethingC")) {
String dataFromC = getIntent.getStringExtra("SomethingC");
}
You can try this code.
Bundle arguments = getArguments();
if (arguments != null){
if (arguments.containsKey("SomethingA")) {
String somethingA = arguments.getString("SomethingA");
if (TextUtils.isEmpty(somethingA)){
// Your codes comes here
}
}
}
Bundle (arguments) can be null if there is no data passed.
To check if string is empty or not use below code :
if(TextUtils.isEmpty(yourString))
{
// String empty
}
else
{
// string not empty
}
In your case you check it as :
if (getIntent()!=null && getIntent().getStringExtra!=null )
{
if (getIntent().hasExtra("SomethingA") && getIntent().hasExtra("SomethingB"))
String dataFromA = getIntent.getStringExtra("SomethingA");
String dataFromB = getIntent.getStringExtra("SomethingB");
}
i am aware that there is a lot of threads about this exception however, i would like people to view my code and assist me in where i am specifically going wrong.
Here is my code for trying to store my users input to an sqlite database and then transfer the code and display it in a new activity. Can anyone tell me where i am going wrong and why this error keeps occuring? (error in title)
Cursor res = myDb.getAllData();
StringBuffer buffer = new StringBuffer();
String getid = res.getString(0);
String getlt = res.getString(1);
String getpt = res.getString(2);
String getqty = res.getString(3);
String getdur = res.getString(4);
String getst = res.getString(5);
String getet = res.getString(6);
if ( res != null && res.moveToFirst()) {
do {
buffer.append(getid);
buffer.append(getlt);
buffer.append(getpt);
buffer.append(getqty);
buffer.append(getdur);
buffer.append(getst);
buffer.append(getet);
} while (res.moveToNext());
}
Intent TransferData = new Intent(getBaseContext(), screen4.class);
TransferData.putExtra("ID", getid);
TransferData.putExtra("LineType", getlt);
TransferData.putExtra("PackageType", getpt);
TransferData.putExtra("Quantity", getqty);
TransferData.putExtra("Duration", getdur);
TransferData.putExtra("Starttime",getst);
TransferData.putExtra("endtime", getet);
startActivity(TransferData);
}
});
}
}
ATTEMPT 2 INSIDE DO BLOCK :
Cursor res = myDb.getAllData();
StringBuffer buffer = new StringBuffer();
if ( res != null && res.moveToNext()) {
do {
String getid = res.getString(0);
String getlt = res.getString(1);
String getpt = res.getString(2);
String getqty = res.getString(3);
String getdur = res.getString(4);
String getst = res.getString(5);
String getet = res.getString(6);
buffer.append(getid);
buffer.append(getlt);
buffer.append(getpt);
buffer.append(getqty);
buffer.append(getdur);
buffer.append(getst);
buffer.append(getet);
Intent TransferData = new Intent(getBaseContext(), screen4.class);
TransferData.putExtra("ID", getid);
TransferData.putExtra("LineType", getlt);
TransferData.putExtra("PackageType", getpt);
TransferData.putExtra("Quantity", getqty);
TransferData.putExtra("Duration", getdur);
TransferData.putExtra("Starttime",getst);
TransferData.putExtra("endtime", getet);
startActivity(TransferData);
} while (res.moveToNext());
What you are trying to do is a unclear.
In your code there is:
String getid = res.getString(0);
String getlt = res.getString(1);
//bla bla bla
I believe they should be inside the do block.
You have tried to access the column data without moving the Cursor to first position.
You must call Cursor#moveToFirst(); before reading any value from the Cursor.
I wanted to display one URL in a table.
Value I had was : www.yahoo.com
I converted into format as below and returning back to table :
www.yahoo.com</font>"> Link
I want it to be displayed in URL form. But it is displaying as a string like as it is above.
Below is the method used for conversion :
private static String validateString(String header) throws Exception
{
System.out.println("&&&&&&&&&& inside validateString ");
String displayString = "";
String colorValue = "";
try
{
if((header) != null && !"null".equalsIgnoreCase(header))
{
displayString = header;
colorValue = "red";
displayString = "<font color=\""+colorValue+"\">"+displayString+"</font>";
}
}
catch(Exception e)
{
displayString = " ";
}
if(!"".equals(displayString))
{
displayString = " Link ";
}
if ("".equals(displayString))
{
displayString = " ";
}
return displayString;
}
Please let me know how to display as URL.
Thanks.
In the attribute href for the tag A, you only should use the proper URL not ant other tag. Change it to something like this:
Link
I am reading CSV file and inserting into table.but when I get completdate as null I want to insert default date.i checked this
if(COMPLETEDATE == null){
css.setString(24, "2013-06-12 00:00:00.0");
}else{
css.setString(24, COMPLETEDATE);
}
Here is the whole function.
public void ImportCSV() {
String csvFile = "C:\\seema\\CSV Files\\2013\\August\\15.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
String PRODLINE,EMPID,EMPFNAME,SHIFT,STATIONID,CURWKDATE,OPCODE,OPDESC,STARTWORKTIME,ENDWORKTIME,PIECECNT,
BUNDLECNT,PIECERATE,SAM,SKILLLEVEL,DAILYBONUS,NORMALRATE,OVERTIMERATE,WORKDURATION,MONO,DESIGNCODE,
DESIGNCOLOR,DESIGNSIZE,COMPLETEDATE;
int i=0;
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
try {
PreparedStatement css = null;
css= conn.prepareStatement("exec uspInsertEWLProduction ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?");
String[] country = line.split(",");
PRODLINE=country[0];
EMPID=country[1];
EMPFNAME =country[2];
SHIFT=country[3];
STATIONID=country[4];
CURWKDATE =country[5];
OPCODE=country[6];
OPDESC=country[7];
STARTWORKTIME =country[8];
ENDWORKTIME=country[9];
PIECECNT=country[10];
BUNDLECNT =country[11];
PIECERATE=country[12];
SAM=country[13];
SKILLLEVEL =country[14];
DAILYBONUS=country[15];
NORMALRATE=country[16];
OVERTIMERATE =country[17];
WORKDURATION=country[18];
MONO=country[19];
DESIGNCODE =country[20];
DESIGNCOLOR=country[21];
DESIGNSIZE=country[22];
COMPLETEDATE =country[23];
if(i!=0) {
css.setString(1, PRODLINE);
css.setString(2, EMPID);
css.setString(3, EMPFNAME);
css.setString(4, SHIFT);
css.setString(5, STATIONID);
css.setString(6, CURWKDATE);
css.setString(7, OPCODE);
css.setString(8, OPDESC);
css.setString(9, STARTWORKTIME);
css.setString(10, ENDWORKTIME);
css.setString(11, PIECECNT);
css.setString(12, BUNDLECNT);
css.setString(13, PIECERATE);
css.setString(14, SAM);
css.setString(15, SKILLLEVEL);
css.setString(16, DAILYBONUS);
css.setString(17, NORMALRATE);
css.setString(18, OVERTIMERATE);
css.setString(19, WORKDURATION);
css.setString(20, MONO);
css.setString(21, DESIGNCODE);
css.setString(22, DESIGNCOLOR);
css.setString(23, DESIGNSIZE);
if(COMPLETEDATE == null) {
css.setString(24, "2013-06-12 00:00:00.0");
} else {
css.setString(24, COMPLETEDATE);
}
}
css.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
i++;
}
JOptionPane.showMessageDialog(null, "Data Imported Successfully");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
The problem is else part is never get executed eventhough copmletedate is null. Any solution?
You can use Apache Commons Utility StringUtils.isEmpty(CharSequence) to check for null or empty string. BTW, why are you storing dates as string, and not as date?
if (StringUtils.isEmpty(COMPLETEDATE)) {
// set default date
} else {
// set COMPLETEDATE
}
try changing if statement to:
if(COMPLETEDATE.equals(""))
You may need to check for null and empty string. Something like this:
if(COMPLETEDATE != null && !("".equals(COMPLETEDATE.trim)))
{
css.setString(24, COMPLETEDATE);
}
else
{
css.setString(24, "2013-06-12 00:00:00.0");
}
Use StringUtils.isBlank() which checks for null or empty string.
visit http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html
U can do one thing that Compare both thing
if(COMPLETEDATE != null || !("".equals(COMPLETEDATE.trim())))
{
css.setString(intlength, xyz);
}
else
{
css.setString(intlength, "Stringdate");
}
May this will help.
and if u Taking loop then try this.
if(country[23] != null && !(country[23].equals(""))){
// set your date code
}else{
// set String date
}
My Java Application produces Null Pointer Exception from JCalander Combobox. I tried to catch the error. But that didnt work. Can someone assist me to fix this. Please.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Calendar.setTime(Calendar.java:1106)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
at java.text.DateFormat.format(DateFormat.java:336)
at org.freixas.jcalendar.JCalendarCombo.paramString(JCalendarCombo.java:780)
at java.awt.Component.toString(Component.java:8095)
tbmodel = (DefaultTableModel)tblItmQty.getModel();
System.out.println(calRecvDate.getDate());
try{
if(calRecvDate.getDate()==null){ // Error
JOptionPane.showMessageDialog(null, "Please Select Shippment Received Date");
calRecvDate.requestFocus();
}else if(txtShipSs.getText().isEmpty()){
////////////////////////////////////////////////////////////////
if (inputValidate() == true) {
try {
String shipId = txtShipId.getText();
String invID = txtInvoice.getText();
String shipSs = txtShipSs.getText();
String address = txtNtfAddress.getText();
String sipper = txtAShipper.getText();
String vessal = txtVessal.getText();
Date rcvDate = calRecvDate.getDate(); // Jcalander
String consignee = txtConsigne.getText();
ArrayList<ShippmentItems> shipItems = new ArrayList<ShippmentItems>();
tbmodel = (DefaultTableModel) tblItmQty.getModel();
for (int i = 0; i < tbmodel.getRowCount(); i++) {
String itmcode = (String) tbmodel.getValueAt(i, 0);
String itmName = (String) tbmodel.getValueAt(i, 1);
int qty = (int) tbmodel.getValueAt(i, 2);
ShippmentItems shpItems = new ShippmentItems(shipId, itmcode, itmName, qty);
shipItems.add(shpItems);
}
Since this throws the NPE:
calRecvDate.getDate()==null
The calRecvDate variable is null, and you will either need to check if it's null before using it, or make sure that it isn't null by tracing back in your code to where you think you've initialized it and fix the problem (since it isn't initialized).
To check if it's null, you could do:
if (calRecvDate != null) {
// use the calRecvDate variable here
} else {
// initialize the calRecvDate variable here
// or perhaps better, display a JOptionPane error message to the user
// that the date hasn't been selected, and exit this method by calling return:
return;
}
Again, don't use try/catch blocks to handle NullPointerExceptions.