java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference The line which causes the error is original.compress(Bitmap.CompressFormat.JPEG, 100, out);
public class MenuScreen extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
dbName1 = Preferences.getStringPreferences(
MenuScreen.this, "DataBaseName");
urlString = new StringBuilder();
urlString.append("UserID=");
urlString.append(Preferences.getintegerPreferences(MenuScreen.this,
"UserId"));
urlString.append("&DbName=");
urlString.append(Preferences.getStringPreferences(MenuScreen.this,
"DataBaseName"));
if (cw == null)
cw = new ConnectToWebService(MenuScreen.this);
getMethod gmLastPunch = getMethod.GetLastPunchInByID;
String url = cw.getUrl(urlString.toString(), gmLastPunch).replaceAll(
" ", "%20");
dialog = ProgressDialog.show(MenuScreen.this, "",
"In Process. Please wait...", false);
new AsyncGetLastPunchIn().execute(url);
if (SerializationUtil.MachinesPermission == null)
SerializationUtil.deserialize(MenuScreen.this);
((Button) findViewById(R.id.btn_Logout)).setOnClickListener(this);
thread = new ProcessThreadUploadImage();
thread.start();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
CheckPendingImages();
handler.postDelayed(this, 2000);
}
}, 2000);
}
public void CheckPendingImages() {
File file = new File(root.getPath());
String[] names = file.list();
if(names!=null)
{
for(String name1 : names)
{
dbName1 = Preferences.getStringPreferences(
MenuScreen.this, "DataBaseName");
if(name1.equals(dbName1))
{
File file1=new File(root.getPath() +"/"+ name1);
if (file1.isDirectory())
{
File[] image = file1.listFiles();
if(image.length!=0)
{
for (int i = 0; i < image.length; i++) {
if (!(image[i].getName()).equals((i + 1) + ".jpeg")) {
count1=image.length;
}
}
}
}
}
}
}
File file2 = new File(root1.getPath());
String[] names2 = file2.list();
if(names2!=null)
{
for(String name3 : names2)
{
if(name3.equals(dbName1))
{
File file3=new File(root1.getPath() +"/"+ name3);
if (file3.isDirectory())
{
File[] image = file3.listFiles();
if(image.length!=0)
{
for (int i = 0; i < image.length; i++) {
if (!(image[i].getName()).equals((i + 1) + ".jpeg")) {
count2=image.length;
}
}
}
}
}
}
}
count=count1+count2;
Pendingimage.setText("Pending Uploads: "+count);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_Logout:
Intent logout = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(logout);
break;
}
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
this.menu = menu;
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
item1 = menu.findItem(R.id.action_punchin);
if (item.getItemId() == android.R.id.home) {
String DatabaseName=Preferences.getStringPreferences(
MenuScreen.this, "DataBaseName");
String PunchID=Preferences.getStringPreferences(MenuScreen.this, "PunchID");
bundle = new Bundle();
Intent upIntent = new Intent(this, ShowDbList.class);
punch = menu.getItem(0);
if ((punch
.getIcon()
.getConstantState()
.equals(getResources().getDrawable(R.drawable.green)
.getConstantState()) || Preferences
.getBooleanPreferences(MenuScreen.this, "IsPunchIn"))) {
bundle.putString("DatabaseName", DatabaseName);
bundle.putString("PunchID", PunchID);
upIntent.putExtras(bundle);
}
startActivity(upIntent);
}
if (item.getItemId() == R.id.action_punchin) {
item1.setEnabled(false);
Intent myIntent = new Intent(this, GPSTrackerNew.class);
Intent LocUploadIntent = new Intent(this, GPSLocationUpload.class);
if ((item
.getIcon()
.getConstantState()
.equals(getResources().getDrawable(R.drawable.red)
.getConstantState()) || !Preferences
.getBooleanPreferences(MenuScreen.this, "IsPunchIn"))) {
Preferences.saveBooleanPreferences(MenuScreen.this,
"IsPunchIn", true);
if (Integer.parseInt(Preferences.getStringPreferences(
MenuScreen.this, "LocationTrackOption")) == 3) {
startService(myIntent);
startService(LocUploadIntent);
}
}
thread = new processThreadServerTime();
thread.start();
}
return super.onOptionsItemSelected(item);
}
public class processThreadGetLastPunchIn extends Thread {
@Override
public void run() {
super.run();
urlString = new StringBuilder();
urlString.append("UserID=");
urlString.append(Preferences.getintegerPreferences(MenuScreen.this,
"UserId"));
urlString.append("&DbName=");
urlString.append(Preferences.getStringPreferences(MenuScreen.this,
"DataBaseName"));
if (cw == null)
cw = new ConnectToWebService(MenuScreen.this);
getMethod gmLastPunch = getMethod.GetLastPunchInByID;
String url = cw.getUrl(urlString.toString(), gmLastPunch)
.replaceAll(" ", "%20");
resultPunchIn = cw.callWebService(url);
handlerLastPunchIn.sendEmptyMessage(0);
}
}
public class processThreadServerTime extends Thread {
@Override
public void run() {
super.run();
if (cw == null)
cw = new ConnectToWebService(MenuScreen.this);
getMethod gm = getMethod.GetApplicationDetails;
String urlLogoutTime = cw.getUrl("", gm).replaceAll(" ", "%20");
dateTime = cw.UpdateVersion(urlLogoutTime);
handlerServerTime.sendEmptyMessage(0);
}
}
public class processThreadServerTime1 extends Thread {
@Override
public void run() {
super.run();
if (cw == null)
cw = new ConnectToWebService(MenuScreen.this);
getMethod gm = getMethod.GetApplicationDetails;
String urlLogoutTime = cw.getUrl("", gm).replaceAll(" ", "%20");
dateTime = cw.UpdateVersion(urlLogoutTime);
handlerServerTime1.sendEmptyMessage(0);
}
}
final Handler handlerLastPunchIn = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
try {
JSONObject jsonObject = new JSONObject(resultPunchIn);
JSONArray jsonArr = jsonObject.getJSONArray("punchList");
if (jsonArr.length() > 0) {
String punchout = jsonArr.getJSONObject(0).getString(
"PunchOutTime");
Preferences.saveStringPreferences(MenuScreen.this,
"PunchId",
jsonArr.getJSONObject(0).getString("PunchID"));
String str = Preferences.getStringPreferences(
MenuScreen.this, "PunchID");
if (punchout == "null")
getMenuInflater().inflate(R.menu.greenpunch, menu);
}
} catch (JSONException e) {
}
}
};
final Handler handlerServerTime = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
try {
JSONObject jsonObject = new JSONObject(dateTime);
Preferences.saveStringPreferences(MenuScreen.this,
"LoginOutTime", jsonObject.getString("DateTimeNow"));
callPunchInOutAPI();
} catch (JSONException e) {
}
}
};
final Handler handlerServerTime1 = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
try {
JSONObject jsonObject = new JSONObject(dateTime);
Preferences.saveStringPreferences(MenuScreen.this,
"LoginOutTime", jsonObject.getString("DateTimeNow"));
callAutomaticPunchInOutAPI();
} catch (JSONException e) {
}
}
};
public void callPunchInOutAPI() {
getMethod gm;
String punchTime = GetImageDateTime();
double strLatitude;
double strLongitude;
Location location = null;
if (gpsTracker.canGetLocation()) {
gpsTracker.getLocation(this);
location = gpsTracker._location;
}
if (location != null) {
strLatitude = location.getLatitude();
strLongitude = location.getLongitude();
}
MenuInflater inflater = getMenuInflater();
if (Preferences.getBooleanPreferences(MenuScreen.this, "IsPunchIn")) {
=
String serverTime = Preferences.getStringPreferences(
MenuScreen.this, "LoginOutTime");
serverTime1=Preferences.getStringPreferences(
MenuScreen.this, "LoginOutTime");
Calendar c = Calendar.getInstance();
String deviceTime = df.format(c.getTime());
try {
Date oldDate = (Date) oldFormatter.parse(Preferences
.getStringPreferences(MenuScreen.this, "LoginOutTime"));
serverTime = newFormatter.format(oldDate);
} catch (ParseException e) {
e.printStackTrace();
}
jsonObjectPunchInData = new JSONObject();
try {
jsonObjectPunchInData.put("UserID", Preferences
.getintegerPreferences(MenuScreen.this, "UserId"));
urlString = new StringBuilder();
urlString.append("DbName=");
urlString.append(Preferences.getStringPreferences(
MenuScreen.this, "DataBaseName"));
gm = getMethod.InsertPunchInData;
new AsyncInsertPunchInOutData().execute(cw.getUrl(
urlString.toString(), gm).replaceAll(" ", "%20"));
}
catch (Exception ex) {
if (dialog != null)
dialog.dismiss();
}
}
}
public void callAutomaticPunchInOutAPI() {
getMethod gm = null;
String punchTime = GetImageDateTime();
double strLatitude;
double strLongitude;
Location location = null;
if (gpsTracker.canGetLocation()) {
gpsTracker.getLocation(this);
location = gpsTracker._location;
}
if (location != null) {
strLatitude = location.getLatitude();
strLongitude = location.getLongitude();
}
MenuInflater inflater = getMenuInflater();
Bundle bundle= getIntent().getExtras();
if(bundle!=null)
{
Boolean punchstatus=bundle.getBoolean("punchstatus");
String PunchedInDatabase=bundle.getString("PunchedInDatabase");
String DatabaseName=bundle.getString("DatabaseName");
String previousdatabasename=bundle.getString("previousdatabasename");
String PunchID=bundle.getString("PunchID");
if(previousdatabasename!=null)
{
jsonObjectPunchInData = new JSONObject();
try {
jsonObjectPunchInData.put("PunchID", PunchID);
jsonObjectPunchInData.put("PunchedOutDatabase", previousdatabasename);
jsonObjectPunchInData.put("PunchedInUserID", Preferences
.getintegerPreferences(MenuScreen.this, "UserId"));
jsonObjectPunchInData.put("PunchedInDatabase", DatabaseName);
jsonObjectPunchInData.put("PunchOutTime", punchTime);
jsonObjectPunchInData.put("PunchOutLatitude", strLatitude);
jsonObjectPunchInData.put("PunchOutLongitude", strLongitude);
urlString = new StringBuilder();
gm = getMethod.UpdatePunchStatusForUser;
}
catch (Exception ex) {
if (dialog != null)
dialog.dismiss();
}
}
}
new AsyncInsertPunchInOutData1().execute(cw.getUrl(
urlString.toString(), gm).replaceAll(" ", "%20"));
}
public class AsyncGetLastPunchIn extends AsyncTask<String, String, String> {
String output;
@Override
protected String doInBackground(String... arg0) {
output = cw.callWebService(arg0[0]);
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dismissProgressDialog();
try {
JSONObject jsonObject = new JSONObject(output);
JSONArray jsonArr = jsonObject.getJSONArray("punchList");
if (jsonArr.length() > 0) {
String punchout = jsonArr.getJSONObject(0).getString(
"PunchOutTime");
Preferences.saveStringPreferences(MenuScreen.this,
"PunchID",
jsonArr.getJSONObject(0).getString("PunchID"));
if (punchout == "null") {
getMenuInflater().inflate(R.menu.greenpunch, menu);
Preferences.saveBooleanPreferences(MenuScreen.this,
"IsPunchIn", true);
if (Integer.parseInt(Preferences.getStringPreferences(
MenuScreen.this, "LocationTrackOption")) == 3)
startService(myIntent);
} else {
getMenuInflater().inflate(R.menu.punch, menu);
Preferences.saveBooleanPreferences(MenuScreen.this,
"IsPunchIn", false);
if (Integer.parseInt(Preferences.getStringPreferences(
MenuScreen.this, "LocationTrackOption")) == 3) {
stopService(myIntent);
gpsTracker.stopRunnable();
}
}
}
} catch (Exception ex) {
}
CheckMenu();
}
}
public class AsyncInsertPunchInOutData extends
AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
if (Preferences.getBooleanPreferences(MenuScreen.this, "IsPunchIn")) {
return cw.postData(params[0], jsonObjectPunchInData);
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dialog != null)
dialog.dismiss();
if (Preferences.getBooleanPreferences(MenuScreen.this, "IsPunchIn")) {
try {
JSONObject jsonObject = new JSONObject(result);
String InsertPunchIn = jsonObject
.getString("InsertPunchInDataResult");
if (Integer.parseInt(InsertPunchIn) != 0) {
Preferences.saveBooleanPreferences(MenuScreen.this,
"IsPunchIn", true);
Preferences.saveStringPreferences(MenuScreen.this,
"PunchID", InsertPunchIn);
MenuItem punch = menu.getItem(0);
punch.setIcon(getResources().getDrawable(
R.drawable.green));
Preferences.saveStringPreferences(MenuScreen.this,
"punch", punch.toString());
Preferences.saveStringPreferences(MenuScreen.this,
"PunchTime", serverTime1);
}
} catch (JSONException e) {
}
}
}
}
public class AsyncInsertPunchInOutData1 extends
AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dialog != null)
dialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(result);
String InsertPunchIn = jsonObject
.getString("UpdatePunchStatusForUserResult");
if (Integer.parseInt(InsertPunchIn) != 0) {
Preferences.saveBooleanPreferences(MenuScreen.this,
"IsPunchIn", true);
Preferences.saveStringPreferences(MenuScreen.this,
"PunchID", InsertPunchIn);
MenuItem punch = menu.getItem(0);
punch.setIcon(getResources().getDrawable(
R.drawable.green));
Preferences.saveStringPreferences(MenuScreen.this,
"punch", punch.toString());
Preferences.saveStringPreferences(MenuScreen.this,
"PunchTime", serverTime1);
}
} catch (JSONException e) {
}
}
}
private String GetImageDateTime() {
String returnVal = "";
try {
String dateInString = Preferences.getStringPreferences(
MenuScreen.this, "LoginOutTime");
Date date = formatter.parse(dateInString);
System.out.println("Date : " + formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
return returnVal;
}
@Override
protected void onResume() {
super.onResume();
if (cw == null)
cw = new ConnectToWebService(MenuScreen.this);
new AsyncLogout(this).execute();
}
private void dismissProgressDialog() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
protected void onDestroy() {
dismissProgressDialog();
super.onDestroy();
}
public void CheckMenu(){
Bundle bundle = getIntent().getExtras();
if(bundle!=null)
{
Boolean punchstatus=bundle.getBoolean("punchstatus");
String PunchedInDatabase=bundle.getString("PunchedInDatabase");
String DatabaseName=bundle.getString("DatabaseName");
String previousdatabasename=bundle.getString("previousdatabasename");
String PunchID=bundle.getString("PunchID");
if(previousdatabasename!=null)
{
if(!DatabaseName.equals(previousdatabasename))
{
punch = menu.getItem(0);
Intent myIntent = new Intent(this, GPSTrackerNew.class);
Intent LocUploadIntent = new Intent(this, GPSLocationUpload.class);
if ((punch
.getIcon()
.getConstantState()
.equals(getResources().getDrawable(R.drawable.red)
.getConstantState()) || !Preferences
.getBooleanPreferences(MenuScreen.this, "IsPunchIn"))) {
Preferences.saveBooleanPreferences(MenuScreen.this,
"IsPunchIn", true);
if (Integer.parseInt(Preferences.getStringPreferences(
MenuScreen.this, "LocationTrackOption")) == 3) {
startService(myIntent);
startService(LocUploadIntent);
}
}
thread = new processThreadServerTime1();
thread.start();
}
}
else if(punchstatus==true && !dbName1.equals(PunchedInDatabase))
{
punch = menu.getItem(0);
Intent myIntent = new Intent(this, GPSTrackerNew.class);
Intent LocUploadIntent = new Intent(this, GPSLocationUpload.class);
if ((punch
.getIcon()
.getConstantState()
.equals(getResources().getDrawable(R.drawable.red)
.getConstantState()) || !Preferences
.getBooleanPreferences(MenuScreen.this, "IsPunchIn"))) {
Preferences.saveBooleanPreferences(MenuScreen.this,
"IsPunchIn", true);
if (Integer.parseInt(Preferences.getStringPreferences(
MenuScreen.this, "LocationTrackOption")) == 3) {
startService(myIntent);
startService(LocUploadIntent);
}
}
thread = new processThreadServerTime1();
thread.start();
}
}
}
public class ProcessThreadUploadImage extends Thread
{
@Override
public void run() {
super.run();
File[] image = root3.listFiles();
File[] image1 = root4.listFiles();
if(image!=null)
{
String[] imgName = root3.list();
String DBName = Preferences.getStringPreferences(
MenuScreen.this, "DataBaseName");
for (int i = 0; i < image.length; i++) {
if (!(image[i].getName()).equals((i + 1) + ".jpeg")) {
Bitmap original = BitmapFactory.decodeFile(image[i].getPath());
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
byte[] b1 = out.toByteArray();
String bitmapstring1 = Base64.encodeToString(b1,Base64.DEFAULT);
jsonuploadimage = new JSONObject();
try {
jsonuploadimage.put("FileData",bitmapstring1);
} catch (JSONException e) {
e.printStackTrace();
}
try {
jsonuploadimage.put("name",imgName[i]);
} catch (JSONException e) {
e.printStackTrace();
}
StringBuffer urlString=new StringBuffer();
urlString.append("&DbName=");
urlString.append(DBName);
if (cw == null)
cw = new ConnectToWebService(
MenuScreen.this);
getMethod gm = getMethod.uploadImages;
thread = new ProcessThreadUploadImage2(cw
.getUrl(urlString.toString(), gm)
.replaceAll(" ", "%20"),
jsonuploadimage);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
if(image1!=null)
{
String[] imgName1 = root4.list();
String DBName = Preferences.getStringPreferences(
MenuScreen.this, "DataBaseName");
for (int i = 0; i < image1.length; i++) {
if (!(image1[i].getName()).equals((i + 1) + ".jpeg")) {
Bitmap original = BitmapFactory.decodeFile(image1[i].getPath());
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
byte[] b1 = out.toByteArray();
String bitmapstring1 = Base64.encodeToString(b1,Base64.DEFAULT);
jsonuploadimage = new JSONObject();
try {
jsonuploadimage.put("FileData",bitmapstring1);
} catch (JSONException e) {
}
try {
jsonuploadimage.put("name",imgName1[i]);
} catch (JSONException e) {
}
StringBuffer urlString=new StringBuffer();
urlString.append("&DbName=");
urlString.append(DBName);
if (cw == null)
cw = new ConnectToWebService(
MenuScreen.this);
getMethod gm = getMethod.uploadImages;
thread = new ProcessThreadUploadImage3(cw
.getUrl(urlString.toString(), gm)
.replaceAll(" ", "%20"),
jsonuploadimage);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}} } }
}
public class ProcessThreadUploadImage2 extends Thread {
String urlPostData2;
JSONObject Jsonuploadimage;
public ProcessThreadUploadImage2(String urlPostData2,
JSONObject jsonuploadimage) {
super();
this.urlPostData2 = urlPostData2;
Jsonuploadimage = jsonuploadimage;
}
@Override
public void run() {
super.run();
if (cw == null)
cw = new ConnectToWebService(MenuScreen.this);
try {
filename= Jsonuploadimage.getString("name");
} catch (JSONException e1) {
}
statusImageUpload = cw.postData(urlPostData2, Jsonuploadimage);
checkStatus();
}
}
public void checkStatus()
{
String ImageUploadResult = null;
if (statusImageUpload != null) {
try {
jsonuploadimage = new JSONObject(statusImageUpload);
} catch (JSONException e) {
e.printStackTrace();
}
try {
ImageUploadResult = jsonuploadimage
.getString("uploadImagesResult");
} catch (JSONException e) {
e.printStackTrace();
}
if (ImageUploadResult != null) {
ImageUploadResult2 = Integer
.parseInt(ImageUploadResult);
if(ImageUploadResult2==1)
{
File file = new File(root3.getPath()+"/"+ filename);
file.delete();
}
}
}
}
public class ProcessThreadUploadImage3 extends Thread {
String urlPostData3;
JSONObject Jsonuploadimage3;
public ProcessThreadUploadImage3(String urlPostData3,
JSONObject jsonuploadimage3) {
super();
this.urlPostData3 = urlPostData3;
Jsonuploadimage3 = jsonuploadimage3;
}
@Override
public void run() {
super.run();
if (cw == null)
cw = new ConnectToWebService(MenuScreen.this);
try {
filename= Jsonuploadimage3.getString("name");
} catch (JSONException e1) {
e1.printStackTrace();
}
statusImageUpload = cw.postData(urlPostData3, Jsonuploadimage3);
checkStatus3();
}
}
public void checkStatus3()
{
String ImageUploadResult = null;
if (statusImageUpload != null) {
try {
jsonuploadimage = new JSONObject(statusImageUpload);
} catch (JSONException e) {
e.printStackTrace();
}
try {
ImageUploadResult = jsonuploadimage
.getString("uploadImagesResult");
} catch (JSONException e) {
e.printStackTrace();
}
if (ImageUploadResult != null) {
ImageUploadResult2 = Integer
.parseInt(ImageUploadResult);
if(ImageUploadResult2==1)
{
File file = new File(root4.getPath()+"/"+ filename);
file.delete();
} }
}
}
private void setMyInt(int value){
int myInt = value;
TextView textView = (TextView) findViewById(R.id.txt_Pending);
textView.setText(String.valueOf("Pending Uploads :"+myInt));
}
}