Error on a Specific Line Number - Android Studio - javascript

No matter how I change that specific line, even if I put nothing in it, or making it a comment, or delete the code all together, the error message keeps telling me that this line causes the error. I even tried deleting the class and copying it all into a new one but still same line numebr is an error.
The code with the error line (109) highlighted:
package com.example.president;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class Game extends AppCompatActivity implements View.OnClickListener {
private Manager gManager;
Player p1,p2,p3;
private ImageView[] hand;
private ImageView[] curr;
private ImageView[] next= new ImageView[3];
private int[] turn = {0, 1, 2};
private int cthrow;
public int[] cards =
{
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7,
R.drawable.c8,
R.drawable.c9,
R.drawable.c10,
R.drawable.c11,
R.drawable.c12,
R.drawable.c13,
R.drawable.c1,
R.drawable.c2,
R.drawable.h3,
R.drawable.h4,
R.drawable.h5,
R.drawable.h6,
R.drawable.h7,
R.drawable.h8,
R.drawable.h9,
R.drawable.h10,
R.drawable.h11,
R.drawable.h12,
R.drawable.h13,
R.drawable.h1,
R.drawable.h2,
R.drawable.s3,
R.drawable.s4,
R.drawable.s5,
R.drawable.s6,
R.drawable.s7,
R.drawable.s8,
R.drawable.s9,
R.drawable.s10,
R.drawable.s11,
R.drawable.s12,
R.drawable.s13,
R.drawable.s1,
R.drawable.s2,
R.drawable.d3,
R.drawable.d4,
R.drawable.d5,
R.drawable.d6,
R.drawable.d7,
R.drawable.d8,
R.drawable.d9,
R.drawable.d10,
R.drawable.d11,
R.drawable.d12,
R.drawable.d13,
R.drawable.d1,
R.drawable.d2,
R.drawable.j1,
R.drawable.j2
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
p1 = new Player("p1");
p2 = new Player("p2");
p3 = new Player("p3");
this.hand = new ImageView[18];
String str;
int resId;
int i;
for (i=0;i<hand.length;i++)
{
str = "card"+i;
resId = getResources().getIdentifier(str, "id", getPackageName());
hand[i]= (ImageView)findViewById(resId);
hand[i].setOnClickListener(this);
}
for (i=0; i<4; i++)
{
str="board"+i;
resId = getResources().getIdentifier(str, "id", getPackageName());
curr[i]= (ImageView)findViewById(resId);
curr[i].setOnClickListener(this);
}
this.gManager = new Manager(this, p1, p2, p3);
this.gManager.handingDeck(p1, p2, p3);
[[[LINE 109]]]
startGame(p1, p2, p3);
}
public void startGame(Player p1, Player p2, Player p3) {
Player p=p1;
int i;
for (i=0; i < 18; i++) {
hand[i].setImageResource(cards[p.getHand().get(i).getIndex()]);
}
String text = p1.getHand().toString();
TextView change = (TextView)findViewById(R.id.textView);
change.setText(text);
}
#Override
public void onClick(View v) {
int i, cnum=0, t=0, resId;
boolean found = false;
Player p=p1;
cthrow=1;
for (i = 0; i < 18 && (!(found)); i++)
{
if (v.getId() == hand[i].getId())
{
String str="card"+cnum;
resId=getResources().getIdentifier(str, "id", getPackageName());
next[turn[t]]= (ImageView)findViewById(resId);
found=true;
curr[cthrow].setImageResource(cards[p.getHand().get(i).getIndex()]);
p.getHand().remove(i);
next[turn[t]].setVisibility(View.INVISIBLE);
if(cnum<10)
cnum=18-cnum;
else
cnum=18-cnum+1;
cthrow++;
}
}
}
}
As you can see the line is even empty.

As pointed out in the comments the error happens actually here:
HERE>> curr[i]= (ImageView)findViewById(resId);
curr[i].setOnClickListener(this);
But it will happen at any line where you use curr as it is not initialized.
For your specific error:
When an error line do not change even if you intentionally change your code the only plausible answer is only one. The actual running code is an old one. So you need to refresh it. How? it depends on where / how you are running it, but a straight forward approach will be to follow this steps incrementally (testing if something changes meanwhile):
Rebuild your project (using your ide rebuild button or by command line)
Clean build your project (ide or cmd as above)
Redeploy your application (in case of an android application 're-install' it on the device where you are testing; emulator or phone)
Uninstall and delete the currently running application (and repeat point 2 & 3)
Reboot your test device (emulator / phone and repeat point 2 & 3)
Reboot your IDE (if you are not sure that all its processes rebooted, reboot your pc)
Delete any build intermediate / cache file your ide may have stored (and repeat 2 & 3)
Nothing worked. Attempt different combinations of the above solutions
If you are here, start thinking about the most dumb stuff and get creative, like:
am I installing the app?
have I stored the modified files?
is the phone connected?
am I modifying the files in the right project?
...
Hope this can help

you have initialized array in a wrong manner, by using new it will initialize array and create memory for that array.
-- so right way to initialize and add values to array is following.
public int[] cards = new int[]{ add your Drawbles here };

Related

How to reflect Firebase Event Listener changes to a DIV element inside a web page [duplicate]

I have a simple HTML page printed out inside a Servlet. Here I have setup firebase admin sdk and have set a value event listener for a path.
When the events fire, I wish to change a Div element and display the results there.
EDIT: The listener does work on further testing with logs.
But I still do not know how to reflect the changes inside the div element as the Listeners are Asynchronous. That's most likely why the script isn't working.
Can someone please guide me as to what I should do. The code is as follows:
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
#WebServlet("/WebWaitlistViewServelet")
public class WebWaitlistViewServelet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WebWaitlistViewServelet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
try {
String cityName=request.getParameter("city");
String restId = request.getParameter("restid");
String userId = request.getParameter("userid");
if(cityName == null || restId == null || userId == null){
System.out.println("Error getting values");
return;
}else{
System.out.println(cityName+" "+restId+" "+userId);
}
pw.println("<HTML>");
pw.println("<HEAD>");
pw.println("<script>");
pw.println("function changePosition(position){ document.getElementById(\"positionInList\").innerHTML = position };");
pw.println("function changeTimeTillTurn(timetillturn){ document.getElementById(\"timeTillTurn\").innerHTML = timetillturn };");
pw.println("function showTimeTillTurn(){ document.getElementById(\"timeDiv\").style.display = \"visible\" };");
pw.println("function hideTimeTillTurn(){ document.getElementById(\"timeDiv\").style.display = \"hidden\" };");
pw.println("</script>");
pw.println("</HEAD>");
pw.println("<BODY>");
pw.println("<DIV align=\"center\">");
pw.println("<B>Your position in the waitlist is:</B>");
pw.println("</DIV><br/>");
pw.println("<DIV id=\"positionInList\" align=\"center\" style=\"color:blue\">");
pw.println("Loading...");
pw.println("</DIV><br/>");
pw.println("<DIV id=\"timeDiv\" align=\"center\">");
pw.println("<B>Approximate time till your turn is: </B><span id=\"timeTillTurn\" style=\"color:blue\">Loading...</span>");
pw.println("</DIV>");
pw.println("<BODY>");
pw.println("</HTML>");
pw.flush();
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/firebaseauth/firebase_admin_sdk_key.json");
// Initialize the app with a service account, granting admin privileges
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(is))
.setDatabaseUrl("https://restaurantrepo.firebaseio.com")
.build();
try {
FirebaseApp.initializeApp(options);
} catch (Exception e) {
//
}
// As an admin, the app has access to read and write all data, regardless of Security Rules
DatabaseReference waitlistRef = FirebaseDatabase.getInstance().getReference().child(cityName).child(restId).child("waitlist");
Comparator<WaitlistedPerson> sortComparator = new Comparator<WaitlistedPerson>() {
#Override
public int compare(WaitlistedPerson lhs, WaitlistedPerson rhs) {
//sort ascending... the bigger the time entered, the later the person has joined... thus the higher the time entered the lower the position
//on waitlist
if(lhs.getTimeentered()<rhs.getTimeentered()){
//if time entered is lower, keep the person higher in the list
return -1;
}else if(lhs.getTimeentered()==rhs.getTimeentered()){
//if the time entered is the same, there are two cases possible
//1.. one person is remotely entered and one has entered at the resto POS... in this case, give priority to POS entered user
//2.. both people have remotely entered ... in this case, give preference to the person with lowest userid (he/she started using our app earlier)
//
//cases that will never happen are
//1.. two people with same userid entered remotely at same time .. can't happen as a second entry simply overwrites the old entry
//2.. two people with same time entered at POS ... can't happen as the resto host can only enter one party at a time..
if(!lhs.isRemotelyEntered() && rhs.isRemotelyEntered()){
//Log.d("FragmentCreate","lhs userid "+lhs.getUserid()+" lhs remotelyentered "+lhs.isRemotelyEntered());
//Log.d("FragmentCreate","rhs userid "+rhs.getUserid()+" rhs remotelyentered "+rhs.isRemotelyEntered());
return -1;
}else if(lhs.isRemotelyEntered() && rhs.isRemotelyEntered()){
//return the lowest user id
//userid is of format Uxx ... so get the xx part begining from index 1 of the string and get the number
int lhsuserid = Integer.parseInt(lhs.getUserid().substring(1));
int rhsuserid = Integer.parseInt(rhs.getUserid().substring(1));
//Log.d("FragmentCreate"," The userids are lhsuserid "+lhsuserid+" rhsuserid"+rhsuserid);
//Do not get tempted to use String compareto function as this will give wrong results
// U11 will get priority over U4 in string compareto due to 1 being lexicographically smaller
//Thus never use lexicographical sorting ever.
//The user ids can never be equal as two remotely entered users will never have two entries (can't... it's impossible due to firebase)
if(lhsuserid<rhsuserid){
return -1;
}else if(lhsuserid==rhsuserid){
//can never happen in real life... two remotely entered users can never have same id ever... just made for safeguard
return 0;
}else{
return 1;
}
}else if(!lhs.isRemotelyEntered() && !rhs.isRemotelyEntered()){
//both entered at POS and have same time
//can never happen in real life...
//made this just for testing scenarios in case i screw up and give wrong inputs
return 0;
}else{
//Log.d("FragmentCreate","lhs userid "+lhs.getUserid()+" lhs remotelyentered "+lhs.isRemotelyEntered());
//Log.d("FragmentCreate","rhs userid "+rhs.getUserid()+" rhs remotelyentered "+rhs.isRemotelyEntered());
return 1;
}
}else{
return 1;
}
}
};
ArrayList<WaitlistedPerson> listOfPeople = new ArrayList<>();
ValueEventListener eventListener = new ValueEventListener(){
#Override
public void onCancelled(DatabaseError error) {
pw.println("<script>hideTimeTillTurn();</script>");
pw.println("<script>changePosition('Sorry, some error occured');</script>");
System.out.println("Sorry. some error occured");
}
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int positionCounter=0;
if(dataSnapshot.getChildrenCount()==1 || dataSnapshot.getChildrenCount() == 0 ){
//Log.d("FragmentCreate","This indicates that the restaurant probably closed down and removed all customers from the list");
pw.println("<script>hideTimeTillTurn();</script>");
pw.println("<script>changePosition('You are no longer in the waitlist');</script>");
System.out.println("You are no longer in the waitlist");
}else{
if(dataSnapshot.hasChild(userId)){
double averageWaitTime=0.0d;
long timeEnteredInMillis=0;
listOfPeople.clear();
Iterable<DataSnapshot> peopleInList = dataSnapshot.getChildren();
for(DataSnapshot currentPerson : peopleInList){
if(currentPerson.getKey().equals("dummy")){
continue;
}
if(currentPerson.getKey().equals(userId)){
//This is our node.... break the loop and enjoy using the counter
averageWaitTime = currentPerson.child("averagewaittimeperparty").getValue(Double.class);
timeEnteredInMillis = currentPerson.child("timeentered").getValue(Long.class);
listOfPeople.add(new WaitlistedPerson(currentPerson));
}else{
listOfPeople.add(new WaitlistedPerson(currentPerson));
}
}
//sort the list using our custom comparator and the get the index
Collections.sort(listOfPeople,sortComparator);
//find the position of the user now
for(WaitlistedPerson person : listOfPeople){
++positionCounter;
if(person.getUserid().equals(userId)){
break;
}
}
double timetillturn = Math.round(averageWaitTime * ((double)positionCounter));
long timeShouldComeAt = timeEnteredInMillis + (long)(timetillturn*60000);
Date timeWhenTurnArrives = new Date(timeShouldComeAt);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
pw.println("<script>showTimeTillTurn();</script>");
pw.println("<script>changePosition('"+positionCounter+"');</script>");
pw.println("<script>changeTimeTillTurn('"+sdf.format(timeWhenTurnArrives)+"');</script>");
System.out.println(positionCounter+" "+sdf.format(timeWhenTurnArrives));
}else{
pw.println("<script>hideTimeTillTurn();</script>");
pw.println("<script>changePosition('You are no longer in the waitlist');</script>");
System.out.println("You are no longer in the waitlist");
}
}
}
};
waitlistRef.addValueEventListener(eventListener);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
pw.close();
} catch (Exception e) {
//do nothing here
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
With servlets, your clients won't see any page updates until they send a new request to your application server. What you can do is get your ValueEventListener to update the state of an in-memory object. Then your servlet can read the latest state of that object whenever constructing the HTML output.
So i managed to solve this using a JSP instead of a Servelet.
In either case, the solution was to tranfer all the logic to a JavaScript function. Then calling the said function on body load.
onload=callthefunction()
The firebase listeners were converted from java to the JavaScript version:
on()
off()
This way there is need to call ajax or anything and the elements that need to be changed can be directly altered from JavaScript

change Dom element based on firebase admin value events

I have a simple HTML page printed out inside a Servlet. Here I have setup firebase admin sdk and have set a value event listener for a path.
When the events fire, I wish to change a Div element and display the results there.
EDIT: The listener does work on further testing with logs.
But I still do not know how to reflect the changes inside the div element as the Listeners are Asynchronous. That's most likely why the script isn't working.
Can someone please guide me as to what I should do. The code is as follows:
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
#WebServlet("/WebWaitlistViewServelet")
public class WebWaitlistViewServelet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WebWaitlistViewServelet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
try {
String cityName=request.getParameter("city");
String restId = request.getParameter("restid");
String userId = request.getParameter("userid");
if(cityName == null || restId == null || userId == null){
System.out.println("Error getting values");
return;
}else{
System.out.println(cityName+" "+restId+" "+userId);
}
pw.println("<HTML>");
pw.println("<HEAD>");
pw.println("<script>");
pw.println("function changePosition(position){ document.getElementById(\"positionInList\").innerHTML = position };");
pw.println("function changeTimeTillTurn(timetillturn){ document.getElementById(\"timeTillTurn\").innerHTML = timetillturn };");
pw.println("function showTimeTillTurn(){ document.getElementById(\"timeDiv\").style.display = \"visible\" };");
pw.println("function hideTimeTillTurn(){ document.getElementById(\"timeDiv\").style.display = \"hidden\" };");
pw.println("</script>");
pw.println("</HEAD>");
pw.println("<BODY>");
pw.println("<DIV align=\"center\">");
pw.println("<B>Your position in the waitlist is:</B>");
pw.println("</DIV><br/>");
pw.println("<DIV id=\"positionInList\" align=\"center\" style=\"color:blue\">");
pw.println("Loading...");
pw.println("</DIV><br/>");
pw.println("<DIV id=\"timeDiv\" align=\"center\">");
pw.println("<B>Approximate time till your turn is: </B><span id=\"timeTillTurn\" style=\"color:blue\">Loading...</span>");
pw.println("</DIV>");
pw.println("<BODY>");
pw.println("</HTML>");
pw.flush();
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/firebaseauth/firebase_admin_sdk_key.json");
// Initialize the app with a service account, granting admin privileges
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(is))
.setDatabaseUrl("https://restaurantrepo.firebaseio.com")
.build();
try {
FirebaseApp.initializeApp(options);
} catch (Exception e) {
//
}
// As an admin, the app has access to read and write all data, regardless of Security Rules
DatabaseReference waitlistRef = FirebaseDatabase.getInstance().getReference().child(cityName).child(restId).child("waitlist");
Comparator<WaitlistedPerson> sortComparator = new Comparator<WaitlistedPerson>() {
#Override
public int compare(WaitlistedPerson lhs, WaitlistedPerson rhs) {
//sort ascending... the bigger the time entered, the later the person has joined... thus the higher the time entered the lower the position
//on waitlist
if(lhs.getTimeentered()<rhs.getTimeentered()){
//if time entered is lower, keep the person higher in the list
return -1;
}else if(lhs.getTimeentered()==rhs.getTimeentered()){
//if the time entered is the same, there are two cases possible
//1.. one person is remotely entered and one has entered at the resto POS... in this case, give priority to POS entered user
//2.. both people have remotely entered ... in this case, give preference to the person with lowest userid (he/she started using our app earlier)
//
//cases that will never happen are
//1.. two people with same userid entered remotely at same time .. can't happen as a second entry simply overwrites the old entry
//2.. two people with same time entered at POS ... can't happen as the resto host can only enter one party at a time..
if(!lhs.isRemotelyEntered() && rhs.isRemotelyEntered()){
//Log.d("FragmentCreate","lhs userid "+lhs.getUserid()+" lhs remotelyentered "+lhs.isRemotelyEntered());
//Log.d("FragmentCreate","rhs userid "+rhs.getUserid()+" rhs remotelyentered "+rhs.isRemotelyEntered());
return -1;
}else if(lhs.isRemotelyEntered() && rhs.isRemotelyEntered()){
//return the lowest user id
//userid is of format Uxx ... so get the xx part begining from index 1 of the string and get the number
int lhsuserid = Integer.parseInt(lhs.getUserid().substring(1));
int rhsuserid = Integer.parseInt(rhs.getUserid().substring(1));
//Log.d("FragmentCreate"," The userids are lhsuserid "+lhsuserid+" rhsuserid"+rhsuserid);
//Do not get tempted to use String compareto function as this will give wrong results
// U11 will get priority over U4 in string compareto due to 1 being lexicographically smaller
//Thus never use lexicographical sorting ever.
//The user ids can never be equal as two remotely entered users will never have two entries (can't... it's impossible due to firebase)
if(lhsuserid<rhsuserid){
return -1;
}else if(lhsuserid==rhsuserid){
//can never happen in real life... two remotely entered users can never have same id ever... just made for safeguard
return 0;
}else{
return 1;
}
}else if(!lhs.isRemotelyEntered() && !rhs.isRemotelyEntered()){
//both entered at POS and have same time
//can never happen in real life...
//made this just for testing scenarios in case i screw up and give wrong inputs
return 0;
}else{
//Log.d("FragmentCreate","lhs userid "+lhs.getUserid()+" lhs remotelyentered "+lhs.isRemotelyEntered());
//Log.d("FragmentCreate","rhs userid "+rhs.getUserid()+" rhs remotelyentered "+rhs.isRemotelyEntered());
return 1;
}
}else{
return 1;
}
}
};
ArrayList<WaitlistedPerson> listOfPeople = new ArrayList<>();
ValueEventListener eventListener = new ValueEventListener(){
#Override
public void onCancelled(DatabaseError error) {
pw.println("<script>hideTimeTillTurn();</script>");
pw.println("<script>changePosition('Sorry, some error occured');</script>");
System.out.println("Sorry. some error occured");
}
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int positionCounter=0;
if(dataSnapshot.getChildrenCount()==1 || dataSnapshot.getChildrenCount() == 0 ){
//Log.d("FragmentCreate","This indicates that the restaurant probably closed down and removed all customers from the list");
pw.println("<script>hideTimeTillTurn();</script>");
pw.println("<script>changePosition('You are no longer in the waitlist');</script>");
System.out.println("You are no longer in the waitlist");
}else{
if(dataSnapshot.hasChild(userId)){
double averageWaitTime=0.0d;
long timeEnteredInMillis=0;
listOfPeople.clear();
Iterable<DataSnapshot> peopleInList = dataSnapshot.getChildren();
for(DataSnapshot currentPerson : peopleInList){
if(currentPerson.getKey().equals("dummy")){
continue;
}
if(currentPerson.getKey().equals(userId)){
//This is our node.... break the loop and enjoy using the counter
averageWaitTime = currentPerson.child("averagewaittimeperparty").getValue(Double.class);
timeEnteredInMillis = currentPerson.child("timeentered").getValue(Long.class);
listOfPeople.add(new WaitlistedPerson(currentPerson));
}else{
listOfPeople.add(new WaitlistedPerson(currentPerson));
}
}
//sort the list using our custom comparator and the get the index
Collections.sort(listOfPeople,sortComparator);
//find the position of the user now
for(WaitlistedPerson person : listOfPeople){
++positionCounter;
if(person.getUserid().equals(userId)){
break;
}
}
double timetillturn = Math.round(averageWaitTime * ((double)positionCounter));
long timeShouldComeAt = timeEnteredInMillis + (long)(timetillturn*60000);
Date timeWhenTurnArrives = new Date(timeShouldComeAt);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
pw.println("<script>showTimeTillTurn();</script>");
pw.println("<script>changePosition('"+positionCounter+"');</script>");
pw.println("<script>changeTimeTillTurn('"+sdf.format(timeWhenTurnArrives)+"');</script>");
System.out.println(positionCounter+" "+sdf.format(timeWhenTurnArrives));
}else{
pw.println("<script>hideTimeTillTurn();</script>");
pw.println("<script>changePosition('You are no longer in the waitlist');</script>");
System.out.println("You are no longer in the waitlist");
}
}
}
};
waitlistRef.addValueEventListener(eventListener);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
pw.close();
} catch (Exception e) {
//do nothing here
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
With servlets, your clients won't see any page updates until they send a new request to your application server. What you can do is get your ValueEventListener to update the state of an in-memory object. Then your servlet can read the latest state of that object whenever constructing the HTML output.
So i managed to solve this using a JSP instead of a Servelet.
In either case, the solution was to tranfer all the logic to a JavaScript function. Then calling the said function on body load.
onload=callthefunction()
The firebase listeners were converted from java to the JavaScript version:
on()
off()
This way there is need to call ajax or anything and the elements that need to be changed can be directly altered from JavaScript

Can a Jbutton (ActionListener) start a static Method?

Im currently coding a Q&A Program, coincidentally called "Jarvis" (Iron Man AI).
The program isnt going to be that complex, so it just answers a few questions and will do simple things like start a song or open the explorer when I write a specific sentence.
I'm the complete opposite of a experienced Java-Programmer,
so I wrote all the important code into a JButton-Actionlistener started Method, the rest of the code is just for design of the application window.
My problem right now is, that my Method,
public void actionPerformed(ActionEvent arg0)
which is started by my JButton ActionListener is just a "public void" Method, so that Im verry limited in my Code.
For example: for some things you need to have a
public static void main (String[]args)
method,
but i dont know how to activate such a method by my actionlistener,
so when I try to start one of these with my actionlistener, there are errors everywhere and even the qickfix removed the "static" from the method.
(If you have any suggestions of making it more efficient, please let me know!)
However, here is my "Program" so far:
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.Color;
import javazoom.jl.player.Player;
import java.io.FileInputStream;
public class JarvisOS {
JFrame JarvisOS;
private JTextField Input;
private JTextField Output;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JarvisOS window = new JarvisOS();
window.JarvisOS.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JarvisOS() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JarvisOS = new JFrame();
JarvisOS.setAlwaysOnTop(true);
JarvisOS.getContentPane().setBackground(SystemColor.window);
JarvisOS.setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\Programme\\Eclipse\\JarvisOS\\JarvisICO.png"));
JarvisOS.setTitle("JarvisOS");
JarvisOS.setBounds(100, 100, 1932, 1368);
JarvisOS.setExtendedState(JFrame.MAXIMIZED_BOTH);
JarvisOS.setVisible(true);
JarvisOS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton Send = new JButton("");
Send.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\SendButton.png"));
Send.setRolloverIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\SendButton Activated.png"));
Send.setBounds(1092, 481, 130, 130);
Send.setForeground(SystemColor.window);
Send.setBorderPainted(false);
JarvisOS.getRootPane().setDefaultButton(Send);
Send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String Text = Input.getText();
if(Text.equalsIgnoreCase("Hello")) {
Output.setText("Hi"); }
else if(Text.equalsIgnoreCase("What are you doing")) {
Output.setText("Nothing Special"); }
else if(Text.equalsIgnoreCase("Do you like me?")) {
Output.setText("Yes"); }
else{
Output.setText("Cant understand that!");}
}
});
JarvisOS.getContentPane().setLayout(null);
Send.setFont(new Font("Arial Black", Font.PLAIN, 16));
Send.setBackground(Color.BLACK);
JarvisOS.getContentPane().add(Send);
JButton RecVoice = new JButton("");
RecVoice.setToolTipText("Record Voice");
RecVoice.setSelectedIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\JarvisOSLogo.png"));
RecVoice.setBackground(Color.BLACK);
RecVoice.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Record Button.png"));
RecVoice.setRolloverIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Record Button Animated.gif"));
RecVoice.setBounds(1598, 698, 296, 298);
RecVoice.setBorderPainted(false);
JarvisOS.getContentPane().add(RecVoice);
Input = new JTextField();
Input.setBorder(javax.swing.BorderFactory.createEmptyBorder());
Input.setBounds(230, 445, 824, 68);
Input.setForeground(SystemColor.window);
Input.setToolTipText("");
Input.setFont(new Font("Arial", Font.BOLD, 40));
Input.setBackground(Color.BLACK);
JarvisOS.getContentPane().add(Input);
Input.setColumns(10);
Output = new JTextField();
Output.setBorder(javax.swing.BorderFactory.createEmptyBorder());
Output.setBounds(230, 584, 824, 68);
Output.setForeground(SystemColor.window);
Output.setEditable(false);
Output.setFont(new Font("Arial", Font.BOLD, 30));
Output.setColumns(10);
Output.setBackground(Color.BLACK);
JarvisOS.getContentPane().add(Output);
JLabel Circle = new JLabel("");
Circle.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Circle.gif"));
Circle.setBounds(1756, 43, 150, 150);
JarvisOS.getContentPane().add(Circle);
JLabel JarvisBackground = new JLabel("");
JarvisBackground.setBounds(0, 0, 1920, 1080);
JarvisBackground.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Jarvis Background.png"));
JarvisOS.getContentPane().add(JarvisBackground);
}
}
Best regards
As noted above, you can call a static method from both a static and non-static context.
However, the method
public static void main (String[] args)
is the entry point to your program (as also noted by the comment above it), so it is unlikely you would want to call that from the action handler of your button. It will be called automatically when you launch java with your JarvisOS class as target (or run from your IDE, which I assume you use since you have quick fixes).
The signature of the main method is fixed, so whatever the quick fix suggests, do not remove the static keyword as that will prevent you from starting your program.
You will need to be more specific about the other "errors everywhere".
Yes, you could do
myButton.addActionListener(e -> myStaticMethod());
Good luck!

infinite scroll in android webview

i have some local html file and i want to show them with infinite scroll method.
NOTE: i cant change the html content, so please don't advice to add javascript to them. i must do it in run time.
so, i figured out that i can execute javascript in runtime via loadUrl("javascript: ....").
i overrided onOverScrolled() method of webView to find out when user reach the end of webView. (it acting carefully, so the problem is not here)
the problem is some times new content attached successfully and other times it didn't geting attached.
in the log i can see that the end of page method get triggered, retrieving new html body get called, executing javascript code get called, but it did not affect.
here is my code, may be something went wrong and i can not see it:
#Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
{
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
if(clampedY & reloadFlag) //for first time realodFlag is false, when the WebViewClient.onPageFinished() get called it turn to ture
{
if (!(isVerticalScrollPossible(SCROLL_DOWN)))
{
reloadFlag = false;
currUri = nextResource(currUri); //findout next page
appendNextPage();
}
}
}
private final int SCROLL_DOWN = 1;
private final int SCROLL_UP = -1;
private boolean isVerticalScrollPossible(int direction)
{
final int offset = computeVerticalScrollOffset();
final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
if (range == 0) return false;
if (direction < 0) {
return offset > 0;
} else {
return offset < range - 1;
}
}
public String getNextPageJS(Uri currPage)
{
String body = getNextPageBody(currPage);
//Log.d("myTAG", body);
String jsResult = "javascript:(function() { document.body.innerHTML += '<div id=\"separator\" style=\"height:10px; margin-top:10px; margin-bottom:10px; background-color:#000000;\"></div>" + body + "';})()";
return jsResult;
}
private void appendNextPage()
{
reloadFlag = false;
Thread appendThread = new Thread(null, doAppend, "backgroundAppend");
appendThread.start();
Log.i("appendNextPage", "get called");
}
public String rs = "";
private Runnable doAppend = new Runnable()
{
#Override
public void run()
{
Log.i("doAppend", "get called + currUri: " + currUri);
rs = getNextPageJS(currUri);
//loadUrl(rs);
appendHandler.sendEmptyMessage(0);
}
};
private Handler appendHandler = new Handler()
{
public void handleMessage(Message msg)
{
loadUrl(rs);
reloadFlag = true;
Log.i("appendHandler", "get called");
}
};
NOTE: sometimes i get this in the emulator log (not in real device):
I/chromium(1339): [INFO:CONSOLE(1)] "Uncaught SyntaxError: An invalid or illegal string was specified.", source: http://localhost:1025/OEBPS/Text/Section0042.xhtml (1)
the number of page is different from time to time, may be it's for bad javasccript code, i don't know.
hints:
1) i'm not javascript coder, so may be the javascript code is not good
2) or maybe calling javascript code several times cause this problem
3) i know that javascript code must execute after page loading completely, so maybe the code called too soon, the problem for this is that onPageFinished() getting called just for first page and it does not called when new content attached via javascript code, i tried to solve this problem using thread, and i think it worked.
UPDATE: i figured out that this code works fine when the html body is small, but when i try to attach large body it didn't work. is loadUrl() method has char limit? or any other idea?
OK, i found the problem, if anyone wants to know.
the problem is that the loadUri() (at least in my case) can not load too many html tag at once (in javascript code i written)
so, the solution is easy, load tags one by one.
here is the code i used:
public ArrayList<String> getNextPageBody(Uri currAddress)
{
String html = getHtml(currAddress); // this is the all html tags in the next file
//get body elements as arrayList, using jsoup
Document doc = Jsoup.parse(html);
Elements elements = doc.select("body").first().children();
ArrayList<String> chuncks = new ArrayList<String>();
for (org.jsoup.nodes.Element el : elements)
{
chuncks.add(el.toString());
}
return chuncks;
}
public void loadBodyChunk(ArrayList<String> bodyChunks)
{
//show a separator for each page
bodyChunks.add(0, "javascript:(function() { document.body.innerHTML += '<div id=\"separator\" style=\"height:10px; margin-top:10px; margin-bottom:10px; background-color:#000000;\"></div>';}())");
loadUrl(bodyChunks.get(0));
for(int i = 1; i < bodyChunks.size(); i++)
{
String jsResult = "javascript:(function() { document.body.innerHTML += '" + bodyChunks.get(i) + "';}())";
loadUrl(jsResult);
}
reloadFlag = true;
}
EDIT:
also:
first the 's in String should be replaced with \' :
body = body.replace("'", "\\'");
then all newline char should be eliminated:
body = body.replaceAll(System.getProperty("line.separator"), " ");
all problem solved.

Phonegap app SQLite db initial set up

What are the best practices to build an app's database schema on first execution?
In other words what I am trying to find out is:
SQLite does not support comma separated queries to be executed as a one statement batch. How to replace that so that the code stays future proof? (I don't see myself putting all the create statements in tx.executeSQL("") chained sequentially it would turn my code into a horrible piece of cr*p).
What I do, in native code as well as in Sencha/Phonegap is to use a DatabaseHelper Class that I refer to. In that class you can see the version of the Database with :
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 2);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
openDataBase();
int cVersion = myDataBase.getVersion();
if(cVersion != 2){
onUpgrade(myDataBase, myDataBase.getVersion(), 2);}
close();
} ....}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
this.getReadableDatabase();
try{
db.execSQL("ADD SQL QUERY HERE TO ADD TABLE");
}
catch(Exception e){}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
Although this is tedious it keeps your database future proof and the query is called at run time. This covers both your needs.
I hope this helps :)

Categories

Resources