All Questions

211
votes
12answers
2.0m views

What is a NullPointerException, and how do I fix it?

What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?What methods/tools can be used to determine the cause so that you stop the exception from causing the program ...
4338
votes
32answers
1.1m views

How do I return the response from an asynchronous call?

I have a function foo which makes an Ajax request. How can I return the response from foo? I tried returning the value from the success callback as well as assigning the response to a local variable ...
2391
votes
23answers
213k views

How to make a great R reproducible example?

When discussing performance with colleagues, teaching, sending a bug report or searching for guidance on mailing lists and here on SO, a reproducible example is often asked and always helpful.What ...
2781
votes
28answers
1.4m views

How can I prevent SQL injection in PHP?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:$unsafe_variable=$_POST['user_input']; ...
1324
votes
35answers
2.3m views

RegEx match open tags except XHTML self-contained tags

I need to match all of these opening tags:<p><a href="http://stackoverflow.com.mevn.net/foo">But not these:<br /><hr class="foo" />I came up with this and wanted to make sure I've got it right. I am ...
1878
votes
33answers
981k views

What is a NullReferenceException, and how do I fix it?

I have some code and when it executes, it throws a NullReferenceException, saying:Object reference not set to an instance of an object.What does this mean, and what can I do to fix this error?
727
votes
23answers
3.2m views

How do I compare strings in Java?

I've been using the==operator in my program to compare all my strings so far.However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.Is==bad? When should it ...
954
votes
26answers
1.4m views

PHP: “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset”

I am running a PHP script, and keep getting errors like:Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10Notice: Undefined index: my_index C:\wamp\www\...
2202
votes
14answers
182k views

Why shouldn't I use mysql_* functions in PHP?

What are the technical reasons for why one shouldn't use mysql_* functions? (e.g. mysql_query(), mysql_connect() or mysql_real_escape_string())?Why should I use something else even if they work on ...
4253
votes
1answer
1.9m views

The Definitive C++ Book Guide and List

This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year.Unlike many other programming languages, which are often picked up on the go from ...
512
votes
14answers
391k views

PHP parse/syntax errors; and how to solve them?

Everyone runs into syntax errors. Even experienced programmers make typos. For newcomers it's just part of the learning process. However, it's often easy to interpret error messages such as:PHP ...
2279
votes
26answers
222k views

Is floating point math broken?

Consider the following code:0.1 + 0.2==0.3 -> false0.1 + 0.2 -> 0.30000000000000004Why do these inaccuracies happen?
2332
votes
38answers
281k views

JavaScript closure inside loops – simple practical example

var funcs=[];for (var i=0; i < 3; i++) { // let's create 3 functionsfuncs[i]=function() { // and store them in funcsconsole.log("My value: " + i); // each should log ...
1199
votes
27answers
418k views

What is an undefined reference/unresolved external symbol error and how do I fix it?

What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them?Feel free to edit/add your own.
2049
votes
27answers
174k views

Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the result of malloc, i.e.int *sieve=malloc(sizeof(int) * length);rather than:int *sieve=(int *) malloc(sizeof(int) * ...
1679
votes
23answers
665k views

Event binding on dynamically created elements?

I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off.This happens on page ready ...
871
votes
31answers
693k views

mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource

I am trying to select data from a MySQL table, but I get one of the following error messages:mysql_fetch_array() expects parameter 1 to be resource, boolean givenormysqli_fetch_array() ...
917
votes
30answers
156k views

Reference - What does this error mean in PHP?

What is this?This is a number of answers about warnings, errors and notices you might encounter while programming PHP and have no clue how to fix. This is also a Community Wiki, so everyone is ...
836
votes
11answers
1.1m views

How to fix “Headers already sent” error in PHP

When running my script, I am getting several errors like this:Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23 ...
381
votes
24answers
240k views

PHP mail function doesn't complete sending of e-mail

<?php$name=$_POST['name'];$email=$_POST['email'];$message=$_POST['message'];$from='From: yoursite.com'; $to='contact@yoursite.com'; $subject='Customer ...
928
votes
7answers
247k views

How to access the correct `this` inside a callback?

I have a constructor function which registers an event handler:function MyConstructor(data, transport) {this.data=data;transport.on('data', function () {alert(this.data); ...
497
votes
11answers
136k views

When to use single quotes, double quotes, and back ticks in MySQL

I am trying to learn the best way to write queries. I also understand the importance of being consistent. Until now, I have randomly used single quotes, double quotes, and back ticks without any real ...
994
votes
14answers
123k views

UTF-8 all the way through

I'm setting up a new server, and want to support UTF-8 fully in my web application. I have tried in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.Where ...
409
votes
12answers
194k views

How to avoid using Select in Excel VBA

I've heard much about the understandable abhorrence of using .Select in Excel VBA, but am unsure of how to avoid using it. I am finding that my code would be more re-usable if I were able to use ...
1903
votes
28answers
361k views

How do you parse and process HTML/XML in PHP?

How can one parse HTML/XML and extract information from it?
5515
votes
78answers
1.6m views

Is Java “pass-by-reference” or “pass-by-value”?

I always thought Java was pass-by-reference.However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't.I don't think I understand the distinction they're making. ...
551
votes
6answers
142k views

Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

Given the following examples, why is outerScopeVar undefined in all cases?var outerScopeVar;var img=document.createElement('img');img.onload=function() {outerScopeVar=this.width;};...
52
votes
1answer
78k views

Reference - What does this regex mean?

What is this?This is a collection of common Q&A. This is also a Community Wiki, so everyone is invited to participate in maintaining it.Why is this?regex is suffering from give me ze code ...
1389
votes
14answers
361k views

Why can templates only be implemented in the header file?

Quote from The C++ standard library: a tutorial and handbook:The only portable way of using templates at the moment is to implement them in header files by using inline functions.Why is this?(...
498
votes
4answers
190k views

SQL injection that gets around mysql_real_escape_string()

Is there an SQL injection possibility even when using mysql_real_escape_string() function?Consider this sample situation. SQL is constructed in PHP like this:$login=mysql_real_escape_string(...
85
votes
1answer
6k views

How to use ThreeTenABP in Android Project

I'm adding this question because I'm new to Java and Android and I searched for hours trying to figure this out. The answer came from a combination of related answers, so I figured I'd document what I ...
1988
votes
49answers
998k views

How do I fix android.os.NetworkOnMainThreadException?

I got an error while running my Android project for RssReader. Code:URL url=new URL(urlToRssFeed);SAXParserFactory factory=SAXParserFactory.newInstance();SAXParser parser=factory....
642
votes
19answers
383k views

Access / process (nested) objects, arrays or JSON

I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)?For example:var data={code: 42,...
1846
votes
8answers
223k views

What is The Rule of Three?

What does copying an object mean?What are the copy constructor and the copy assignment operator?When do I need to declare them myself?How can I prevent my objects from being copied?
484
votes
4answers
43k views

Why is iostream::eof inside a loop condition considered wrong?

I just found a comment in this answer saying that using iostream::eof in a loop condition is "almost certainly wrong". I generally use something like while(cin>>n) - which I guess implicitly ...
7654
votes
88answers
1.3m views

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves?I ...
2703
votes
73answers
3.2m views

How can I get query string values in JavaScript?

Is there a plugin-less way of retrieving query string values via jQuery (or without)? If so, how? If not, is there a plugin which can do so?
484
votes
9answers
102k views

The Use of Multiple JFrames: Good or Bad Practice? [closed]

I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI. I'm ...
368
votes
6answers
85k views

Why does jQuery or a DOM method such as getElementById not find the element?

What are the possible reasons for document.getElementById, $("#id") or any other DOM method / jQuery selector not finding the elements?Example problems include:jQuery silently failing to bind an ...
457
votes
5answers
178k views

Why is “while ( !feof (file) )” always wrong?

I've seen people trying to read files like this in a lot of posts lately.Code#include <stdio.h>#include <stdlib.h>int main(int argc, char **argv){char * path=argc > 1 ? ...
2077
votes
36answers
645k views

Why is “using namespace std” considered bad practice?

I've been told by others that writing using namespace std in code is wrong, and that I should use std::cout and std::cin directly instead.Why is using namespace std considered a bad practice? Is it ...
396
votes
4answers
30k views

What is the difference between client-side and server-side programming?

I have this code:<script type="text/javascript">var foo='bar';<?phpfile_put_contents('foo.txt', ' + foo + ');?>var baz=<?php echo 42; ?>;...
624
votes
18answers
319k views

Unfortunately MyApp has stopped. How can I solve this?

I am developing an application, and everytime I run it, I get the message:Unfortunately, MyApp has stopped.What can I do to solve this?About this question - obviously inspired by What is a ...
206
votes
19answers
461k views

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

What does ArrayIndexOutOfBoundsException mean and how do I get rid of it? Here is a code sample that triggers the exception:String[] name={"tom", "dick", "harry"};for(int i=0; i<=name....
1077
votes
20answers
346k views

How does the “this” keyword work?

I have noticed that there doesn't appear to be a clear explanation of what the this keyword is and how it is correctly (and incorrectly) used in JavaScript on the Stack Overflow site.I have ...
490
votes
15answers
322k views

Scanner is skipping nextLine() after using next() or nextFoo()?

I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this:System.out.println("Enter numerical value"); int option;option=input.nextInt(); // Read ...
874
votes
20answers
248k views

Can a local variable's memory be accessed outside its scope?

I have the following code.#include <iostream>int * foo(){int a=5;return &a;}int main(){int* p=foo();std::cout << *p;*p=8;std::cout << ...
422
votes
15answers
115k views

How to test multiple variables against a value?

I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say:x=0...
906
votes
6answers
117k views

Where and why do I have to put the “template” and “typename” keywords?

In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code:template <typename T, typename Tail> //...
2062
votes
30answers
115k views

“Least Astonishment” and the Mutable Default Argument

Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue:def foo(a=[]):a.append(5)return aPython novices would expect this function to always ...

153050per page