java.lang.nullpointerexception

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

java.lang.nullpointerexception

Postby Lena » Tue Jun 21, 2016 12:44 am

Hi.
Sometimes, rarely when running the application on android I get an error: java.lang.nullpointerexception
To fix this I need to turn off the bluetooth device and then on again. Than restart app.
Where it may be a bug in my code?
Code: Select all
//h
private:   // User declarations
public:      // User declarations
     std::vector<String> events;
      bool flag;
   __fastcall TForm1(TComponent* Owner);


Code: Select all
//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "UnitMikros.h"
#include <stdlib.h>
#include <System.StartupCopy.hpp>
#include <IdSSLOpenSSLHeaders.hpp>

#include "UnitServis.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
#pragma resource ("*.NmXhdpiPh.fmx", _PLAT_ANDROID)
#pragma resource ("*.iPhone4in.fmx", _PLAT_IOS)
#pragma resource ("*.LgXhdpiPh.fmx", _PLAT_ANDROID)
#pragma link "System.StartupCopy"


TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
//may be problem here?
//may be use OnCreate?
 DateEdit3->Date = Now();
 DateEdit1->Date = Now();
 DateEdit2->Date = Now();

 events.push_back(L"Вход в зону");
 events.push_back(L"Отметка");
 events.push_back(L"Выход из зоны");

 Beacon1->Enabled = true;
 flag = true;
 Timer1->Enabled = false;

   #if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || defined(__ANDROID__)
   MediaPlayer1->FileName =
      IncludeTrailingPathDelimiter(System::Ioutils::TPath::GetDocumentsPath()) +
      "sound.m4a";
   #else
      MediaPlayer1->FileName = "sound.m4a";
   #endif

 IdOpenSSLSetLibPath(System::Ioutils::TPath::GetDocumentsPath());
}
//---------------------------------------------------------------------------

void ExceptionMessage(Exception &E)
{
  struct TCloseDialogHandler : public TCppInterfacedObject<TInputCloseDialogProc>
         {
          void __fastcall Invoke(const System::Uitypes::TModalResult AResult)
           {
            switch (AResult)
            {
               case mrYes :
                {
                 //Application->Terminate();
                 _exit(0);
                 //ShowMessage("You chose Yes");
                }
               break;
               case mrNo:
                //ShowMessage("You chose No");
               break;
               case mrCancel:
                //ShowMessage("You chose Cancel");
               break;
            }
           }
         };

      _di_TInputCloseDialogProc handler = new TCloseDialogHandler();
      String MES = L"Произошла ошибка. Класс: " + E.ClassName() + L"\nСообщение: " + E.Message + L"\nПрограмма будет закрыта.";
      MessageDlg(MES, TMsgDlgType::mtInformation,   TMsgDlgButtons() << TMsgDlgBtn::mbYes, 0, handler);

}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)
{
 try
   {
      FDQuerySelectFromLog->Close();
     String FormatDATA = FormatDateTime("yyyy-mm-dd", DateEdit3->DateTime);
     FDQuerySelectFromLog->ParamByName(L"dateparam")->AsString = FormatDATA;//DateEdit3->Date.DateString();
     FDQuerySelectFromLog->Open();
     if(FDQuerySelectFromLog->RecordCount == 0)
       {
         FDQuerySelectFromLog->Close();

         ShowMessage(L"Нет отметок в этот день.");
       }
   }
   catch(Exception &E)
      {
       ExceptionMessage(E);
      }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button4Click(TObject *Sender)
{
  try
   {

       if(DateEdit1->DateTime > DateEdit2->DateTime)
      {
        ShowMessage(L"Дата ОТ должна быть меньше даты ДО!");
        return;
      }


     FDQuerySelestLogPeriod->Close();

     String FormatDATAstart = FormatDateTime("yyyy-mm-dd", DateEdit1->DateTime);
     String FormatDATAend = FormatDateTime("yyyy-mm-dd", DateEdit2->DateTime);

     FDQuerySelestLogPeriod->ParamByName(L"dateparamstart")->AsString = FormatDATAstart;
     FDQuerySelestLogPeriod->ParamByName(L"dateparamend")->AsString = FormatDATAend;

     FDQuerySelestLogPeriod->Open();

     if(FDQuerySelestLogPeriod->RecordCount == 0)
       {
         FDQuerySelestLogPeriod->Close();
         ShowMessage(L"Нет отметок за этот период.");
       }
   }
   catch(Exception &E)
      {
       ExceptionMessage(E);
      }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Beacon1BeaconProximity(TObject * const Sender, IBeacon * const ABeacon,
          TBeaconProximity Proximity)
{

try
  {

  switch ( ABeacon->Proximity )
   {
      case TBeaconProximity::Immediate:
         {

          if(flag == true)
          {
                      FDQueryInsertInLog->ParamByName(L"idbeacon")->AsString = NULL;
           FDQueryInsertInLog->ParamByName(L"minor")->AsInteger = NULL;
           FDQueryInsertInLog->ParamByName(L"datetimeevents")->AsString = NULL;
           FDQueryInsertInLog->ParamByName(L"events")->AsString = NULL;
           FDQueryInsertInLog->ParamByName(L"dateparam")->AsString = NULL;

           //FDQueryInsertInLog->ParamByName(L"idbeacon")->AsString = ABeacon->DeviceIdentifier;
           FDQueryInsertInLog->ParamByName(L"minor")->AsInteger = ABeacon->GetMinor();
           TDateTime DataInLogTable = Now();
           FDQueryInsertInLog->ParamByName(L"datetimeevents")->AsString = DataInLogTable.DateTimeString();
           //FDQueryInsertInLog->ParamByName(L"events")->AsString = events[1];
           String FormatDATA = FormatDateTime("yyyy-mm-dd", DataInLogTable);
           FDQueryInsertInLog->ParamByName(L"dateparam")->AsString = FormatDATA;//DataInLogTable.DateString();
           FDQueryInsertInLog->ExecSQL();

            flag = false;
            Timer1->Enabled = true;
            if(FileExists(MediaPlayer1->FileName) == false)
              {
               ShowMessage(L"Отметка сохранена но не найден звуковой файл!");
               return;
              }
              MediaPlayer1->Play();
          }


         }
         break;

      case TBeaconProximity::Near:
         //Memo1->Lines->Add(L"Возле" );
         break;

      case TBeaconProximity::Far:
         //Memo1->Lines->Add(L"Далеко" );
         break;

      case TBeaconProximity::Away:
         //Memo1->Lines->Add(L"Очень далеко" );
         break;

   };
  }
   catch(Exception &E)
      {
       ExceptionMessage(E);
      }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FDConnection1BeforeConnect(TObject *Sender)
{
 #if defined(_PLAT_IOS) || defined(_PLAT_ANDROID)
 FDConnection1->Params->Values["Database"] =
 System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetDocumentsPath
 (), "mikros.s3db");
 #endif
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

 FormServis = new TFormServis(this);
 FormServis->StyleBook = Form1->StyleBook1;
 FormServis->Show();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
 flag = true;
 Timer1->Enabled = false;
}
//---------------------------------------------------------------------------


C++ Berlin.
Lena
BCBJ Master
BCBJ Master
 
Posts: 444
Joined: Sun Feb 06, 2011 1:28 pm

Re: java.lang.nullpointerexception

Postby rlebeau » Tue Jun 21, 2016 10:41 am

Lena wrote:Sometimes, rarely when running the application on android I get an error: java.lang.nullpointerexception
To fix this I need to turn off the bluetooth device and then on again. Than restart app.
Where it may be a bug in my code?


It is hard to answer that, as you threw a bunch of code here but did not indicate which line of code is actually raising the error. Have you tried debugging the code yet?

Lena wrote:
Code: Select all
//may be use OnCreate?



Do not use the OnCreate event in C++.

Lena wrote:
Code: Select all
FDQueryInsertInLog->ParamByName(L"idbeacon")->AsString = NULL;
FDQueryInsertInLog->ParamByName(L"datetimeevents")->AsString = NULL;
FDQueryInsertInLog->ParamByName(L"events")->AsString = NULL;
FDQueryInsertInLog->ParamByName(L"dateparam")->AsString = NULL;



Not related to your crash, but do you realize that NULL is defined as integer 0? So you are actually assigning the string value "0" to your fields, not a blank string. Is that what you really want?

Why are you initializing the fields to NULL just to turn around and immediately assign new values to them afterwards?
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1394
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: java.lang.nullpointerexception

Postby Lena » Tue Jun 21, 2016 11:56 pm

Have you tried debugging the code yet?

It is difficult to catch this exception. I can run the application 20 times a day for several days and had no problems. An exception occurs very rarely only when the application starts.
Helps disable then enable Bluetooth, after this application is restarted again normally. Maybe it is a problem with the component TBeacon.
If I got this error in my app I try starting other application from market. For example: https://play.google.com/store/apps/deta ... roid&hl=ru
This app is no longer determining the Beacon. Helps disable then enable Bluetooth on my devices after this applications determining my Beacon: http://www.banggood.com/Bytereal-iBeaco ... 65539.html

Why are you initializing the fields to NULL just to turn around and immediately assign new values to them afterwards?

It is my habit to work with parameters in FireDAC. :)
Lena
BCBJ Master
BCBJ Master
 
Posts: 444
Joined: Sun Feb 06, 2011 1:28 pm

Re: java.lang.nullpointerexception

Postby rlebeau » Wed Jun 22, 2016 12:43 am

Lena wrote:If I got this error in my app I try starting other application from market. For example: https://play.google.com/store/apps/deta ... roid&hl=ru
This app is no longer determining the Beacon. Helps disable then enable Bluetooth on my devices after this applications determining my Beacon: http://www.banggood.com/Bytereal-iBeaco ... 65539.html


Sounds like the underlying Bluetooth system on your device is buggy. Do you have all available Android updates installed?
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1394
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: java.lang.nullpointerexception

Postby Lena » Wed Jun 22, 2016 1:03 am

I got this rare error on two different phone models when my app try starts:
GSmart Akta A4 (Android 4.4.2)
Fly (I do not remember the model)
It seems that the TBeacon support starts with version Android 4.3
Lena
BCBJ Master
BCBJ Master
 
Posts: 444
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 10 guests

cron