Repository: adessose/budgeteer
Issue: Budget without Contract - details NPE

In case I have a budget without an associated contract and actual work records exist on this budget, Budgeteer warns, "The budget is not assigned to a contract. Please assign a contract."

Clicking on this warning sometimes leads to Edit Budget Details, sometimes, however, it leads to an internal server error.

See [budgeteer.log.txt](https://github.com/adessoAG/budgeteer/files/2827511/budgeteer.log.txt) attached for the server side stacktrace.


Changed files:
budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/overview/BudgetsOverviewPage.java
budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/BudgeteerSession.java
budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/edit/form/EditBudgetForm.java

diff --git a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/BudgeteerSession.java b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/BudgeteerSession.java
index 5c4116a9..b0180985 100644
--- a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/BudgeteerSession.java
+++ b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/BudgeteerSession.java
@@ -13,6 +13,7 @@ import org.wickedsource.budgeteer.web.pages.templates.TemplateFilter;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
+import java.util.ArrayList;
 import java.util.HashMap;
 
 public class BudgeteerSession extends WebSession {
@@ -87,16 +88,24 @@ public class BudgeteerSession extends WebSession {
     }
 
     public BudgetTagFilter getBudgetFilter() {
-        return budgetFilter.get(getProjectId());
+        BudgetTagFilter filter = budgetFilter.get(projectId);
+
+        if (filter == null) {
+            filter = new BudgetTagFilter(new ArrayList<>(), projectId);
+            setBudgetFilter(filter);
+        }
+
+        return filter;
     }
 
     public void setBudgetFilter(BudgetTagFilter budgetTagFilter) {
         this.budgetFilter.put(projectId, budgetTagFilter);
     }
+
     public TemplateFilter getTemplateFilter() {
-        if(templateFilter.get(getProjectId()) == null){
+        if (templateFilter.get(getProjectId()) == null) {
             return new TemplateFilter(getProjectId());
-        }else {
+        } else {
             return templateFilter.get(getProjectId());
         }
     }
@@ -140,8 +149,7 @@ public class BudgeteerSession extends WebSession {
      * Sets the given {@link Authentication} in the current {@link SecurityContext}
      * and saves the context in the session.
      *
-     * @param authToken
-     *          The {@link Authentication} to set in the {@link SecurityContext}.
+     * @param authToken The {@link Authentication} to set in the {@link SecurityContext}.
      */
     private void setAuthInSecurityContext(Authentication authToken) {
 
diff --git a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/edit/form/EditBudgetForm.java b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/edit/form/EditBudgetForm.java
index 98b08ca9..f974892b 100644
--- a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/edit/form/EditBudgetForm.java
+++ b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/edit/form/EditBudgetForm.java
@@ -92,7 +92,7 @@ public class EditBudgetForm extends Form<EditBudgetData> {
                     public Object getDisplayValue(ContractBaseData object) {
                         return object == null ? getString("no.contract") : object.getContractName();
                     }
-                }){
+                }) {
             @Override
             protected String getNullValidDisplayValue() {
                 return EditBudgetForm.this.getString("no.contract");
diff --git a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/overview/BudgetsOverviewPage.java b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/overview/BudgetsOverviewPage.java
index 5692152e..1af12168 100644
--- a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/overview/BudgetsOverviewPage.java
+++ b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/web/pages/budgets/overview/BudgetsOverviewPage.java
@@ -9,7 +9,6 @@ import org.apache.wicket.markup.html.link.Link;
 import org.apache.wicket.request.mapper.parameter.PageParameters;
 import org.apache.wicket.spring.injection.annot.SpringBean;
 import org.wickedsource.budgeteer.service.budget.BudgetService;
-import org.wickedsource.budgeteer.service.budget.BudgetTagFilter;
 import org.wickedsource.budgeteer.web.BudgeteerSession;
 import org.wickedsource.budgeteer.web.Mount;
 import org.wickedsource.budgeteer.web.components.links.NetGrossLink;
@@ -27,8 +26,6 @@ import org.wickedsource.budgeteer.web.pages.budgets.overview.table.FilteredBudge
 import org.wickedsource.budgeteer.web.pages.budgets.weekreport.multi.MultiBudgetWeekReportPage;
 import org.wickedsource.budgeteer.web.pages.dashboard.DashboardPage;
 
-import java.util.ArrayList;
-
 import static org.wicketstuff.lazymodel.LazyModel.from;
 import static org.wicketstuff.lazymodel.LazyModel.model;
 
@@ -40,15 +37,11 @@ public class BudgetsOverviewPage extends BasePage {
 
     public BudgetsOverviewPage() {
         BudgetTagsModel tagsModel = new BudgetTagsModel(BudgeteerSession.get().getProjectId());
-        if (BudgeteerSession.get().getBudgetFilter() == null) {
-            BudgetTagFilter filter = new BudgetTagFilter(new ArrayList<>(), BudgeteerSession.get().getProjectId());
-            BudgeteerSession.get().setBudgetFilter(filter);
-        }
         add(new BudgetRemainingFilterPanel("remainingFilter", new RemainingBudgetFilterModel(BudgeteerSession.get().getProjectId())));
         add(new BudgetTagFilterPanel("tagFilter", tagsModel));
         FilteredBudgetModel filteredBudgetModel = new FilteredBudgetModel(BudgeteerSession.get().getProjectId(), model(from(BudgeteerSession.get().getBudgetFilter())));
         filteredBudgetModel.setRemainingFilterModel(model(from(BudgeteerSession.get().getRemainingBudgetFilterValue())));
-        add(new BudgetOverviewTable("budgetTable", filteredBudgetModel ,getBreadcrumbsModel()));
+        add(new BudgetOverviewTable("budgetTable", filteredBudgetModel, getBreadcrumbsModel()));
         add(new BookmarkablePageLink<MultiBudgetWeekReportPage>("weekReportLink", MultiBudgetWeekReportPage.class));
         add(new BookmarkablePageLink<MultiBudgetMonthReportPage>("monthReportLink", MultiBudgetMonthReportPage.class));
         add(createNewBudgetLink("createBudgetLink"));
@@ -59,11 +52,11 @@ public class BudgetsOverviewPage extends BasePage {
 
     private Component createReportLink(String string) {
         Link link = new Link(string) {
-			@Override
-			public void onClick() {
-				setResponsePage(new BudgetReportPage(BudgetsOverviewPage.class, new PageParameters()));
-			}
-		};
+            @Override
+            public void onClick() {
+                setResponsePage(new BudgetReportPage(BudgetsOverviewPage.class, new PageParameters()));
+            }
+        };
 
         if (!budgetService.projectHasBudgets(BudgeteerSession.get().getProjectId())) {
             link.setEnabled(false);
@@ -71,7 +64,7 @@ public class BudgetsOverviewPage extends BasePage {
             link.add(new AttributeModifier("title", BudgetsOverviewPage.this.getString("links.budget.label.no.budget")));
         }
         return link;
-	}
+    }
 
     private Link createNewBudgetLink(String id) {
         return new Link(id) {