In the second iteration in the FOR loop, onwards, object returns me null
I have a variable that gets the instance of a database (ROOM) and does a select, returning object, CidVo.
private AppDataBase appDataBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atendimento_tab);
appDataBase = AppDataBase.getInstance(this);
}
In a return of an asynchronous task, I make this query. I need the CidVo corresponding to what was returned to me by the asynchronous task, the CidVo's are recorded in the database and yes, there are correspondences of them in that base.
The problem is in the second iteration onwards of the FOR loop.
Why does it happen?
For loop:
@Override
public void retornoAsyncTaskResultPredict(AsyncTaskResult<Retorno> asyncTaskResult) {
if (asyncTaskResult.getExceptionResult() == null) {
RetornoPredicaoCid predicaoCidVo = (RetornoPredicaoCid) asyncTaskResult.getResult();
if (predicaoCidVo.getRetorno() != null) {
List<PredicaoCidVo> predCid = predicaoCidVo.getRetorno();
List<CidVo> predictedText = new ArrayList<>();
List<CidVo> predictedText1 = new ArrayList<>();
for (int i = 0;i<predCid.size();i++) {
//appDataBase = AppDataBase.getInstance(this);
CidVo cidVo = appDataBase.getCidDao().getCidVo(predCid.get(i).getTextPredicted());
CidVo cidVo1 = appDataBase.getCid(this, predCid.get(i).getTextPredicted());
predictedText1.add(cidVo1);
predictedText.add(cidVo);
}
}
}
AppDataBase:
@Database(entities = {CidVo.class}, version = 8, exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {
public abstract CidDao getCidDao();
private static AppDataBase appDataBase;
public static AppDataBase getInstance(Context context) {
if (null == appDataBase) {
appDataBase = buildDataBaseInstance(context);
}
return appDataBase;
}
private static AppDataBase buildDataBaseInstance(Context context) {
return Room.databaseBuilder(context,
AppDataBase.class,
"AutoCompleteVo")
.fallbackToDestructiveMigration()
.allowMainThreadQueries().build();
}
//CID predicted
public CidVo getCid(Context context, String idCidVo) {
if (appDataBase == null) {
appDataBase = AppDataBase.getInstance(context);
}
return appDataBase.getCidDao().getCidVo("%" + idCidVo + "%");
}
}
DAO:
@Dao
public interface CidDao {
//CID
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllCID(List<CidVo> cidVos);
@Query("Select * from CidVo WHERE idCid = :idCid")
CidVo getCidVo(String idCid);
}
The first iteration of the loop returns normally, but from the second on, returns null, the select.
Edit:
Edit:
If I select the variable (appdatabase
) (ALT + F8) and place the code path manually, the query returns the data to me.
java

add a comment |
I have a variable that gets the instance of a database (ROOM) and does a select, returning object, CidVo.
private AppDataBase appDataBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atendimento_tab);
appDataBase = AppDataBase.getInstance(this);
}
In a return of an asynchronous task, I make this query. I need the CidVo corresponding to what was returned to me by the asynchronous task, the CidVo's are recorded in the database and yes, there are correspondences of them in that base.
The problem is in the second iteration onwards of the FOR loop.
Why does it happen?
For loop:
@Override
public void retornoAsyncTaskResultPredict(AsyncTaskResult<Retorno> asyncTaskResult) {
if (asyncTaskResult.getExceptionResult() == null) {
RetornoPredicaoCid predicaoCidVo = (RetornoPredicaoCid) asyncTaskResult.getResult();
if (predicaoCidVo.getRetorno() != null) {
List<PredicaoCidVo> predCid = predicaoCidVo.getRetorno();
List<CidVo> predictedText = new ArrayList<>();
List<CidVo> predictedText1 = new ArrayList<>();
for (int i = 0;i<predCid.size();i++) {
//appDataBase = AppDataBase.getInstance(this);
CidVo cidVo = appDataBase.getCidDao().getCidVo(predCid.get(i).getTextPredicted());
CidVo cidVo1 = appDataBase.getCid(this, predCid.get(i).getTextPredicted());
predictedText1.add(cidVo1);
predictedText.add(cidVo);
}
}
}
AppDataBase:
@Database(entities = {CidVo.class}, version = 8, exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {
public abstract CidDao getCidDao();
private static AppDataBase appDataBase;
public static AppDataBase getInstance(Context context) {
if (null == appDataBase) {
appDataBase = buildDataBaseInstance(context);
}
return appDataBase;
}
private static AppDataBase buildDataBaseInstance(Context context) {
return Room.databaseBuilder(context,
AppDataBase.class,
"AutoCompleteVo")
.fallbackToDestructiveMigration()
.allowMainThreadQueries().build();
}
//CID predicted
public CidVo getCid(Context context, String idCidVo) {
if (appDataBase == null) {
appDataBase = AppDataBase.getInstance(context);
}
return appDataBase.getCidDao().getCidVo("%" + idCidVo + "%");
}
}
DAO:
@Dao
public interface CidDao {
//CID
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllCID(List<CidVo> cidVos);
@Query("Select * from CidVo WHERE idCid = :idCid")
CidVo getCidVo(String idCid);
}
The first iteration of the loop returns normally, but from the second on, returns null, the select.
Edit:
Edit:
If I select the variable (appdatabase
) (ALT + F8) and place the code path manually, the query returns the data to me.
java

The application runs normally, and I get returned in the query, in the first iteration, the corresponding data, but from the second on, even if there are items, it returns me null.
– Henrique
Nov 20 '18 at 11:51
add a comment |
I have a variable that gets the instance of a database (ROOM) and does a select, returning object, CidVo.
private AppDataBase appDataBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atendimento_tab);
appDataBase = AppDataBase.getInstance(this);
}
In a return of an asynchronous task, I make this query. I need the CidVo corresponding to what was returned to me by the asynchronous task, the CidVo's are recorded in the database and yes, there are correspondences of them in that base.
The problem is in the second iteration onwards of the FOR loop.
Why does it happen?
For loop:
@Override
public void retornoAsyncTaskResultPredict(AsyncTaskResult<Retorno> asyncTaskResult) {
if (asyncTaskResult.getExceptionResult() == null) {
RetornoPredicaoCid predicaoCidVo = (RetornoPredicaoCid) asyncTaskResult.getResult();
if (predicaoCidVo.getRetorno() != null) {
List<PredicaoCidVo> predCid = predicaoCidVo.getRetorno();
List<CidVo> predictedText = new ArrayList<>();
List<CidVo> predictedText1 = new ArrayList<>();
for (int i = 0;i<predCid.size();i++) {
//appDataBase = AppDataBase.getInstance(this);
CidVo cidVo = appDataBase.getCidDao().getCidVo(predCid.get(i).getTextPredicted());
CidVo cidVo1 = appDataBase.getCid(this, predCid.get(i).getTextPredicted());
predictedText1.add(cidVo1);
predictedText.add(cidVo);
}
}
}
AppDataBase:
@Database(entities = {CidVo.class}, version = 8, exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {
public abstract CidDao getCidDao();
private static AppDataBase appDataBase;
public static AppDataBase getInstance(Context context) {
if (null == appDataBase) {
appDataBase = buildDataBaseInstance(context);
}
return appDataBase;
}
private static AppDataBase buildDataBaseInstance(Context context) {
return Room.databaseBuilder(context,
AppDataBase.class,
"AutoCompleteVo")
.fallbackToDestructiveMigration()
.allowMainThreadQueries().build();
}
//CID predicted
public CidVo getCid(Context context, String idCidVo) {
if (appDataBase == null) {
appDataBase = AppDataBase.getInstance(context);
}
return appDataBase.getCidDao().getCidVo("%" + idCidVo + "%");
}
}
DAO:
@Dao
public interface CidDao {
//CID
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllCID(List<CidVo> cidVos);
@Query("Select * from CidVo WHERE idCid = :idCid")
CidVo getCidVo(String idCid);
}
The first iteration of the loop returns normally, but from the second on, returns null, the select.
Edit:
Edit:
If I select the variable (appdatabase
) (ALT + F8) and place the code path manually, the query returns the data to me.
java

I have a variable that gets the instance of a database (ROOM) and does a select, returning object, CidVo.
private AppDataBase appDataBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atendimento_tab);
appDataBase = AppDataBase.getInstance(this);
}
In a return of an asynchronous task, I make this query. I need the CidVo corresponding to what was returned to me by the asynchronous task, the CidVo's are recorded in the database and yes, there are correspondences of them in that base.
The problem is in the second iteration onwards of the FOR loop.
Why does it happen?
For loop:
@Override
public void retornoAsyncTaskResultPredict(AsyncTaskResult<Retorno> asyncTaskResult) {
if (asyncTaskResult.getExceptionResult() == null) {
RetornoPredicaoCid predicaoCidVo = (RetornoPredicaoCid) asyncTaskResult.getResult();
if (predicaoCidVo.getRetorno() != null) {
List<PredicaoCidVo> predCid = predicaoCidVo.getRetorno();
List<CidVo> predictedText = new ArrayList<>();
List<CidVo> predictedText1 = new ArrayList<>();
for (int i = 0;i<predCid.size();i++) {
//appDataBase = AppDataBase.getInstance(this);
CidVo cidVo = appDataBase.getCidDao().getCidVo(predCid.get(i).getTextPredicted());
CidVo cidVo1 = appDataBase.getCid(this, predCid.get(i).getTextPredicted());
predictedText1.add(cidVo1);
predictedText.add(cidVo);
}
}
}
AppDataBase:
@Database(entities = {CidVo.class}, version = 8, exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {
public abstract CidDao getCidDao();
private static AppDataBase appDataBase;
public static AppDataBase getInstance(Context context) {
if (null == appDataBase) {
appDataBase = buildDataBaseInstance(context);
}
return appDataBase;
}
private static AppDataBase buildDataBaseInstance(Context context) {
return Room.databaseBuilder(context,
AppDataBase.class,
"AutoCompleteVo")
.fallbackToDestructiveMigration()
.allowMainThreadQueries().build();
}
//CID predicted
public CidVo getCid(Context context, String idCidVo) {
if (appDataBase == null) {
appDataBase = AppDataBase.getInstance(context);
}
return appDataBase.getCidDao().getCidVo("%" + idCidVo + "%");
}
}
DAO:
@Dao
public interface CidDao {
//CID
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllCID(List<CidVo> cidVos);
@Query("Select * from CidVo WHERE idCid = :idCid")
CidVo getCidVo(String idCid);
}
The first iteration of the loop returns normally, but from the second on, returns null, the select.
Edit:
Edit:
If I select the variable (appdatabase
) (ALT + F8) and place the code path manually, the query returns the data to me.
java

java

edited Nov 20 '18 at 12:14
Henrique
asked Nov 20 '18 at 11:34
HenriqueHenrique
226
226
The application runs normally, and I get returned in the query, in the first iteration, the corresponding data, but from the second on, even if there are items, it returns me null.
– Henrique
Nov 20 '18 at 11:51
add a comment |
The application runs normally, and I get returned in the query, in the first iteration, the corresponding data, but from the second on, even if there are items, it returns me null.
– Henrique
Nov 20 '18 at 11:51
The application runs normally, and I get returned in the query, in the first iteration, the corresponding data, but from the second on, even if there are items, it returns me null.
– Henrique
Nov 20 '18 at 11:51
The application runs normally, and I get returned in the query, in the first iteration, the corresponding data, but from the second on, even if there are items, it returns me null.
– Henrique
Nov 20 '18 at 11:51
add a comment |
1 Answer
1
active
oldest
votes
In the second iteration on, the items came to come with a space at the beginning or end, causing the problem. Solved with a simple .trim();
predCid.get(i).getTextPredicted ().trim()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392126%2fin-the-second-iteration-in-the-for-loop-onwards-object-returns-me-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the second iteration on, the items came to come with a space at the beginning or end, causing the problem. Solved with a simple .trim();
predCid.get(i).getTextPredicted ().trim()
add a comment |
In the second iteration on, the items came to come with a space at the beginning or end, causing the problem. Solved with a simple .trim();
predCid.get(i).getTextPredicted ().trim()
add a comment |
In the second iteration on, the items came to come with a space at the beginning or end, causing the problem. Solved with a simple .trim();
predCid.get(i).getTextPredicted ().trim()
In the second iteration on, the items came to come with a space at the beginning or end, causing the problem. Solved with a simple .trim();
predCid.get(i).getTextPredicted ().trim()
answered Nov 20 '18 at 13:46
HenriqueHenrique
226
226
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392126%2fin-the-second-iteration-in-the-for-loop-onwards-object-returns-me-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The application runs normally, and I get returned in the query, in the first iteration, the corresponding data, but from the second on, even if there are items, it returns me null.
– Henrique
Nov 20 '18 at 11:51